Im trying upload large file in ASP.net MVC and next save in DB. But when I do this I have "outofmemoryexception". Here I post my file:
[HttpPost]
public void UploadAttachments(int docId, int folderId)
{
if (Request.Files.Count <= 0) return; //throw exception?
for (var i = 0; i < Request.Files.Count; i++)
{
try
{
var file = Request.Files[i];
ProjectAttachmentInput attachment = new ProjectAttachmentInput();
attachment = SetAttachment(attachment, file, docId, folderId);
_documentationAppService.SaveUploadedFile(attachment);
}
catch { }
}
}
Next in SetAttachment():
private ProjectAttachmentInput SetAttachment(ProjectAttachmentInput attachment, HttpPostedFileBase file, int docId, int folderId)
{
attachment.FileName = file.FileName;
attachment.FileContent = ReadFully(file.InputStream);
attachment.ProjectAttachmentId = docId;
attachment.ProjectAttachmentFolderId = folderId;
return attachment;
}
And when i try ReadFully I get OutOfMemoryException...
private static byte[] ReadFully(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
Error is when i try input.CopyTo(ms) I need byte[] from file and next save this in DB. This works if the file is less than 100MB. But when I try 200MB I have exception..
What should change in this code?
And my web.config configuration:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
...
<httpRuntime maxRequestLength="1073741824" targetFramework="4.5.1" />