Reputation: 3
I what to read the bytes of a docx file and I have this method:
public ActionResult Create(Model myModel, HttpPostedFileBase fileUpload)
{
MemoryStream ms = new MemoryStream();
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fileUpload.InputStream.Length;
int len;
while (rdlen < totlen)
{
len = fileUpload.InputStream.Read(bin, 0, 100);
ms.Write(bin, 0, len);
rdlen += len;
}
}
the total length of the file is 11338 but it only reads until 11326 then it stuck in an infinite loop because when it reaches the 11326 this len = fileUpload.InputStream.Read(bin, 0, 100);
will only return 0 as a value. The weird thing is that if I upload a txt file it work as it should.
Thanks
Upvotes: 0
Views: 883
Reputation: 2507
This works, im using it myself for images uploaded.
public ActionResult Create(Model myModel, HttpPostedFileBase fileUpload)
{
byte[] Data = null;
using (var binaryReader = new BinaryReader(fileUpload.InputStream))
{
Data = binaryReader.ReadBytes(fileUpload.ContentLength);
}
}
Upvotes: 0
Reputation: 86
byte[] myFile;
using (var memoryStream = new MemoryStream())
{
httpPostedFileBase.InputStream.CopyTo(memoryStream);
myFile = memoryStream.ToArray();// or use .GetBuffer() as suggested by Morten Anderson
}
Upvotes: 1