tags:

views:

150

answers:

7

My client is uploading file more then 1 GB through application. I know i can only upload only 100mb using asp.net MVC application.

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

i am getting error at byte[] data = new byte[st.Length]; because st.Length=1330768612 Error - "Exception of type 'System.OutOfMemoryException' was thrown."

Is there any way i can upload more then 1gb file? Why we can define maxRequestLength= 0 - 2097151 in webconfig,

A: 

Hi Pankaj

I remember on an older project we had to work out a way to allow the user to upload a 2-4gb file for our ASP.NET web application.

If I recall correctly, we used the 'File Upload Control' and edited the web.config to allow for a greater file size:

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

Another option would be to use the HttpModule:

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

Dal
Thanks @@Dal.. i am alreading this setting on config file <httpRuntime executionTimeout="7200" maxRequestLength="2097151"/>
Pankaj
@Pankaj - ooo Sorry, forgot about your last line :)
Dal
+3  A: 

IMO you need to use the right tool for the job. Http was simply not intended to transfer large files like this. Why dont you use ftp instead, and maybe you could then build a web interface around that.

DarenFox
I agree, the requirement needs a different tool
Martin Clarke
And you don't think that might cause issues? If you want to use a web interface which will place the file on the FTP you will still be using http. When you want to do this you must use plugins (java applet, Flash, SilverLight but that won't be easy) to really use FTP since you can't rely on browsers for uploading to FTP (afaik only IE can be used to put files on an FTP since it can use explorer.exe to show FTP contents). Other downside is that you might need to give access to users on your FTP (or supply settings in some way).
Gertjan
There are entire application categories like document management, WCM, all types of online services that require the ability to upload large files and can't use FTP. Besides, the issue isn't the tool or MVC, it's just a bug in a couple of lines of code.
Panagiotis Kanavos
+1  A: 

The error shown to you suggests the server has not enough memory to process the file in memory. Validate if your server has enough memory to allocate such a big array/file.

You could also try to process chuncks of the stream. The fact that you get an out of memory suggests that the file is sent to the server, but the server cannot process the file.

I really think it has to do with the size of the array you allocate. It just won't fit in the memory of you machine (of in the memory assigned to .NET).

Gertjan
+1  A: 

The error says that you run out of memory while trying to allocate a 1GB byte array in memory. This is not related to MVC. You should also note that the memory limit for 32bit processes is 2GB. If your server runs a 32bit OS and you allocate 1GB of that for a single upload you will quickly deplete the available memory.

Instead of trying to read the entire stream in memory, use Stream.Read to read the data in chuncks using a reasonably sized buffer and store the chuncks to a file stream with a Write call. Not only will you avoid OutOfMemoryExceptions, your code will also run much faster, because you won't have to wait to load the entire 1GB before storing it to a file.

The code can be as simple as this:

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 tweak the buffer sizes to balance throughput (how quickly you upload and save) vs scalability (how many clients you can handle).

Panagiotis Kanavos
A: 

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

Jeroen
A: 

The link provides a way to FTP files from ASP .NET http://www.devasp.net/net/articles/display/280.html

Hari Chinnan
A: 

Here's a Microsoft Knowledge base answer for you

Tom Gullen