I have to upload large file on server side via AJAX in asp.net mvc application. In view i divide the file into chunks and send one chunk at a time and merge all chunks in controller but the merged file not opens and it consists on some bytes more than orignal file. Suggest me if you have any better solution or plugin.I followed the following tutorial How to upload large file My code is
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");
}