[英]C# MVC - uploading multiple large files creating byte array
有人可以通過將大型文檔轉換為字節數組來幫助我上傳大型文檔(多個)嗎?
我的代碼當前沒有字節數組,但是如果文檔很大,它當然會失敗。
控制器:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Invoice invoice){if(ModelState.IsValid){List fileDetails = new List(); for(int i = 0; i <Request.Files.Count; i ++){var file = Request.Files [i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FileDetail fileDetail = new FileDetail()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
fileDetails.Add(fileDetail);
var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"),
fileDetail.Id + fileDetail.Extension);
file.SaveAs(path);
}
}
invoice.FileDetails = fileDetails;
db.Invoices.Add(invoice);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(invoice);
}
和表單元素:
任何幫助都感激不盡。
排序!
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Invoice invoice){if(ModelState.IsValid){List fileDetails = new List();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase httpPostedFileBase = Request.Files[i];
if (httpPostedFileBase != null)
{
Stream stream = httpPostedFileBase.InputStream;
BinaryReader bReader = new BinaryReader(stream);
byte[] bytes = bReader.ReadBytes((Int32)stream.Length);
}
HttpPostedFileBase postedFileBase = Request.Files[i];
if (postedFileBase != null)
{
var fileName = Path.GetFileName(postedFileBase.FileName);
FileDetail fileDetail = new FileDetail()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
fileDetails.Add(fileDetail);
//Save the Byte Array as File.
var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"),
fileDetail.Id + fileDetail.Extension);
postedFileBase.SaveAs(path);
postedFileBase.InputStream.Flush();
}
}
invoice.FileDetails = fileDetails;
db.Invoices.Add(invoice);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(invoice);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.