在MVC中使用JQuery AJAX上传文件

3
我需要通过ASP.NET MVC应用程序中的AJAX在服务器端上传大文件。在视图中,我将文件分为多个块,逐个发送一个块,并在控制器中合并所有块,但是合并后的文件无法打开,并且它的大小比原始文件多一些字节。如果您有更好的解决方案或插件,请建议我。我遵循了以下教程如何上传大文件我的代码如下:
public ContentResult UploadBigFiles()
    {
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            string fileName = hpf.FileName;
            byte[] Buffer = new byte[hpf.ContentLength];

            var chunks = hpf.InputStream;

            string path = Server.MapPath("~/Uploads/Temp");
            string newpath = Path.Combine(path, fileName);
            string[] filePaths = Directory.GetFiles(path);
            if (filePaths.Count() == 0 && !Directory.Exists(newpath))
            {
                using (System.IO.FileStream fs = System.IO.File.Create(newpath))
                {
                    byte[] bytes = new byte[hpf.ContentLength];

                    int bytesRead;
                    while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
                    {
                        fs.Write(bytes, 0, bytesRead);
                    }
                }
            }
            else
            {
                var buffer = new byte[hpf.ContentLength];

                //using (System.IO.FileStream input = System.IO.File.Create(Server.MapPath("~/Uploads/Temp/" + hpf.FileName + "2nd")))
                {
                    byte[] bytes = new byte[hpf.ContentLength];

                    int bytesRead;
                    //while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
                    //{
                        //input.Write(bytes, 0, bytesRead);
                   // }


                    // using (var input = System.IO.File.Create(Server.MapPath("~/Uploads/Temp/" + hpf.FileName+"2nd")))
                    //using (var input = System.IO.File.Open(Server.MapPath("~/Uploads/Temp/" + hpf.FileName + "2nd"), FileMode.Open))
                    {
                        // var output = System.IO.File.Create(Server.MapPath("~/Uploads/output.txt"));
                        var inputFile = System.IO.File.Open(Server.MapPath("~/Uploads/Temp/" + hpf.FileName), FileMode.Append);
                        //var buffer = new byte[hpf.ContentLength];
                        // int bytesRead = buffer.Length;
                        while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            inputFile.Write(buffer, 0, bytesRead);
                        }
                    }
                }
                System.IO.File.Delete(Server.MapPath("~/Uploads/Temp/" + hpf.FileName + "2nd"));
            }


            if (hpf.ContentLength == 0)
                continue;
            string pathForSaving = Server.MapPath("~/Uploads");
            if (this.CreateFolderIfNeeded(pathForSaving))
            {
                try
                {
                    hpf.SaveAs(Path.Combine(pathForSaving, hpf.FileName));
                    //MergeFiles1
                    //isUploaded = true;
                    //message = "File uploaded successfully!";
                }
                catch (Exception ex)
                {
                    //message = string.Format("File upload failed: {0}", ex.Message);
                }
            }
            //string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName));
            //hpf.SaveAs(savedFileName); // Save the file

            //r.Add(new ViewDataUploadFilesResult()
            //{
            //    Name = hpf.FileName,
            //    Length = hpf.ContentLength,
            //    Type = hpf.ContentType
            //});
        }
        // Returns json
        return Content("{\"name\":\"" + Request.Files[0].FileName + "\",\"type\":\"" + Request.Files[0].ContentType + "\",\"size\":\"" + string.Format("{0} bytes", Request.Files[0].ContentLength) + "\"}", "application/json");
    }
1个回答

2

while循环看起来不正确。 你读取数据到 bytes[] 数组中...

while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)

但是你从 buffer[] 数组中写入文件。

inputFile.Write(buffer, 0, bytesRead);

这可能会解释你的文件中存在随机数据的原因。

也许可以尝试以下方法:

while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
    inputFile.Write(bytes, 0, bytesRead);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接