How to Create Byte Array from Httppostedfile

How to create byte array from HttpPostedFile

Use a BinaryReader object to return a byte array from the stream like:

byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}

How do I get a byte array from HttpInputStream for a docx file?

Turns out that since I am using the stream already (see the controller method in the question), it is empty when I tried to save it.

I am not sure why I experienced this with docx and not xlsx since they both have their Streams consumed before the save. My guess is it has something to do with the differences in the Aspose.Cells and Aspose.Words implementations.

Regardless, however, I set the position on the stream back to 0, and it worked.

//viewmodel.File is HttpPostedFileBase

viewModel.File.InputStream.Position = 0; //<-----This fixed it!

byte[] fileData;
using (var binaryReader = new BinaryReader(viewModel.File.InputStream))
{
fileData = binaryReader.ReadBytes(viewModel.File.ContentLength);
}

Cannot convert HttpPostedFileBase to Byte[]

The answer ended up being the memoryStream == null check, as JoeEnos suggested. MemoryStream is instantiated on the line directly above this check - so it should never be null - and should be checked with if (memoryStream.Length == 0) instead.

Is it possible to convert byte [] to HttpPostedFile?

If you look at the source for the class you'll see a few things.

  1. The constructor is internal, so you'll never directly construct an instance.
  2. The class has minimal functionality.
  3. The class is sealed, so you can't add your own constructor using inheritance.

Instead of trying to convert a byte array into an instance of HttpPostedFile, you'll have a much smoother experience by converting your downstream methods to accept byte[] as input, or maybe Stream. If you need the other properties of HttpPostedFile, then you could just send them in directly as additional parameters, or write your own class to wrap them up.

Convert HttpPostedFileBase to byte[]

As Darin says, you can read from the input stream - but I'd avoid relying on all the data being available in a single go. If you're using .NET 4 this is simple:

MemoryStream target = new MemoryStream();
model.File.InputStream.CopyTo(target);
byte[] data = target.ToArray();

It's easy enough to write the equivalent of CopyTo in .NET 3.5 if you want. The important part is that you read from HttpPostedFileBase.InputStream.

For efficient purposes you could check whether the stream returned is already a MemoryStream:

byte[] data;
using (Stream inputStream = model.File.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}

Can not convert HttpPostedFileBase object into byte array

You need to seek to the start of the stream first, before copying it:

ProfilePhoto.InputStream.Seek(0, SeekOrigin.Begin);
ProfilePhoto.InputStream.CopyTo(ms);

How to convert byte array to HttpPostedFileBase in C#?

 public class HttpPostedFileBaseCustom : HttpPostedFileBase
{
private byte[] _Bytes;
private String _ContentType;
private String _FileName;
private MemoryStream _Stream;

public override Int32 ContentLength { get { return this._Bytes.Length; } }
public override String ContentType { get { return this._ContentType; } }
public override String FileName { get { return this._FileName; } }

public override Stream InputStream
{
get
{
if(this._Stream == null)
{
this._Stream = new MemoryStream(this._Bytes);
}
return this._Stream;
}
}

public HttpPostedFileBaseCustom(byte[] contentData, String contentType, String fileName)
{
this._ContentType = contentType;
this._FileName = fileName;
this._Bytes = contentData ?? new byte[0];
}

public override void SaveAs(String filename)
{
System.IO.File.WriteAllBytes(filename, this._Bytes);
}
}

how to convert a byte[] to HttpPostedFileBase using c#

What about creating a custom postedfile? :)

public class MemoryPostedFile : HttpPostedFileBase
{
private readonly byte[] fileBytes;

public MemoryPostedFile(byte[] fileBytes, string fileName = null)
{
this.fileBytes = fileBytes;
this.FileName = fileName;
this.InputStream = new MemoryStream(fileBytes);
}

public override int ContentLength => fileBytes.Length;

public override string FileName { get; }

public override Stream InputStream { get; }
}

That you can simply use like this:

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(bytes);

How to convert a file into byte array in memory?

As opposed to saving the data as a string (which allocates more memory than needed and might not work if the binary data has null bytes in it), I would recommend an approach more like

foreach (var file in files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
var fileBytes = ms.ToArray();
string s = Convert.ToBase64String(fileBytes);
// act on the Base64 data
}
}
}

Also, for the benefit of others, the source code for IFormFile can be found on GitHub

How do I read the correct Stream/byte[] from HttpPostedFile InputStream property?

Noticed that this question wasn't answered (since Evk's comment was the solution) and I couldn't accept any answer.

So I'm making this one just to not leave this question unanswered.

tl;dr;
The solution as per Evk's comment was the position of the stream, it was being read beforehand and setting the position to 0 before trying to create the pdf was enough to fix the problem.



Related Topics



Leave a reply



Submit