Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 4从HttpPostedFileBase读取字节_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# MVC 4从HttpPostedFileBase读取字节

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

我知道如何读取docx文件的字节,我有以下方法:

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; 内伦; 而(rdlen 文件的总长度是11338,但它只读取到11326,然后它卡在一个无限循环中,因为当它到达11326时,这个
len=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);
    }
}