我必须在asp.net mvc应用程序中通过AJAX在服务器端上传大文件。在视图中,我将文件分成块并一次发送一个块并合并控制器中的所有块,但合并的文件不会打开,它包含一些字节而不是orignal文件。 建议我,如果你有更好的解决方案或插件。我遵循以下教程 How to upload large file 我的代码是
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");
}
答案 0 :(得分: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);