How can I upload a 1.25 GB file size in ASP.NET MVC

My client uploads a file of more than 1 GB through the application. I know that I can only download 100mb with an asp.net MVC application.

public static byte[] ReadStream(Stream st) { st.Position = 0; byte[] data = new byte[st.Length]; . . . . } 

I get an error when byte[] data = new byte[st.Length]; because st.Length=1330768612 Error - "An exception of type" System.OutOfMemoryException "was thrown."

Is there a way to upload more than 1gb file? Why can we define maxRequestLength = 0 - 2097151 in webconfig,

+4
source share
6 answers

IMO you need to use the right tool for the job. Http was simply not meant to transfer large files like this. Why don't you use ftp instead, and perhaps you could create a web interface for this.

+3
source

The above error assumes that the server does not have enough memory to process the file in memory. Confirm if there is enough memory on your server to host such a large array / file.

You can also try to handle stream chuncks. The fact that you get out of memory indicates that the file is being sent to the server, but the server cannot process the file.

I really think this is due to the size of the allocated array. It simply does not fit into your computer’s memory (from memory allocated by .NET).

+1
source

The error indicates that you ran out of memory while trying to allocate a 1 GB byte array in memory. This is not related to MVC. It should also be noted that the memory limit for 32-bit processes is 2 GB. If your server has a 32-bit OS installed and you allocate 1 GB for one boot, you will quickly run out of available memory.

Instead of trying to read the entire stream in memory, use Stream.Read to read data in chuncks using a buffer of sufficient size and store chuncks in the file stream by calling Write. You not only avoid OutOfMemoryExceptions, but your code will work much faster, because you do not have to wait until only 1 GB to load before storing it in a file.

The code may be simple:

 public static void SaveStream(Stream st,string targetFile) { byte[] inBuffer = new byte[10000]; using(FileStream outStream=File.Create(targetFile,20000)) using (BinaryWriter wr = new BinaryWriter(outStream)) { st.Read(inBuffer, 0, inBuffer.Length); wr.Write(inBuffer); } } 

You can adjust the buffer sizes to balance bandwidth (how fast you load and save) and scalability (how many clients you can handle).

+1
source

I remember that in an older project, we had to develop a method that allows the user to upload a 2-4gb file for our ASP.NET web application.

If I remember correctly, we used "File Upload Control" and edited the web.config file to increase the file size:

 <system.web> <httpRuntime executionTimeout="2400" maxRequestLength="40960" /> </system.web> 

Another option is to use the HttpModule:

http://dotnetslackers.com/Community/blogs/haissam/archive/2008/09/12/upload-large-files-in-asp-net-using-httpmodule.aspx

0
source

I would suggest using FTP or writing a small desktop application. HTTP was never going to send such large files.

0
source
0
source

Source: https://habr.com/ru/post/1315034/


All Articles