C# MVC 4从HttpPostedFileBase读取字节
我知道如何读取docx文件的字节,我有以下方法:C# MVC 4从HttpPostedFileBase读取字节,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我知道如何读取docx文件的字节,我有以下方法: public ActionResult Create(Model myModel, HttpPostedFileBase fileUpload) { MemoryStream ms = new MemoryStream(); byte[] bin = new byte[100]; lo
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;
}
}
公共操作结果创建(模型myModel、HttpPostedFileBase文件上载)
{
MemoryStream ms=新的MemoryStream();
字节[]bin=新字节[100];
长rdlen=0;
long totlen=fileUpload.InputStream.Length;
内伦;
而(rdlenlen=fileUpload.InputStream.Read(bin,0100)代码>将仅返回0作为值。奇怪的是,如果我上传一个txt文件,它就会正常工作
谢谢这很有效,我自己用它上传图片
public ActionResult Create(Model myModel, HttpPostedFileBase fileUpload)
{
byte[] Data = null;
using (var binaryReader = new BinaryReader(fileUpload.InputStream))
{
Data = binaryReader.ReadBytes(fileUpload.ContentLength);
}
}
你能展示完整的动作吗?方法很长,但这是它给我带来麻烦的部分。fileUpload是什么类型的?HttpPostedFileBase docx文件这可能是正确的答案,但很难说为什么,甚至在没有解释的情况下回答OP的问题。你能详细说明一下吗?
public ActionResult Create(Model myModel, HttpPostedFileBase fileUpload)
{
byte[] Data = null;
using (var binaryReader = new BinaryReader(fileUpload.InputStream))
{
Data = binaryReader.ReadBytes(fileUpload.ContentLength);
}
}