tags:

views:

680

answers:

5

I need to upload large files of atleast 1GB file size. I am using ASP.Net, C# and IIS 5.1 as my development platform.

I am using HIF.PostedFile.InputStream.Read(fileBytes,0,HIF.PostedFile.ContentLength) before using File.WriteAllBytes(filePath, fileByteArray)(doesnt go here but gives System.OutOfMemoryException' exception)

Currently i have set the httpRuntime to executionTimeout="999999" maxRequestLength="2097151"(thats 2GB!) useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="5000" enableVersionHeader="true" requestLengthDiskThreshold="8192" Also i have set maxAllowedContentLength="2097151" (guess its only for IIS7)

I have changed IIS connection timeout to 999,999 secs too.

I am unable to upload files of even 4578KB(Ajaz-Uploader.zip)

A: 

Try copying without loading every thing in the memory :

public void CopyFile()
{
    Stream source = HIF.PostedFile.InputStream; //your source file
    Stream destination = File.OpenWrite(filePath); //your destination
    Copy(source, destination);
}

public static long Copy(Stream from, Stream to)
{
    long copiedByteCount = 0;

    byte[] buffer = new byte[2 << 16];
    for (int len; (len = from.Read(buffer, 0, buffer.Length)) > 0; )
    {
        to.Write(buffer, 0, len);
        copiedByteCount += len;
    }
    to.Flush();

    return copiedByteCount;
}

Hope this help.

Manitra Andriamitondra
Hello manitra,I tried using the your functions CopyFile() in client and Copy() in server, but i got some errors.One more thing i have observed is that WriteAllBytes will work until 3MB of data, more than that it gives "System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: There was an exception running the extensions specified in the config file. ---> System.Web.HttpException: Maximum request length exceeded....." exception.
Ramya Raj
A: 

Check this blog entry about large file uploads. It also has a few links to some discussion forums that can shed some light on this as well. The suggestion is to use custom HttpHandler for that or custom Flash/Silverlight control.

Good luck.

Audrius
A: 

I googled and found - NeatUpload


Another solution would be to read the bytes on the client and send it to the server, the server saves the file. Example

Server: in Namespace - Uploader, class - Upload

[WebMethod]
public bool Write(String fileName, Byte[] data)
{
    FileStream  fs = File.Open(fileName, FileMode.Open);
    BinaryWriter bw = new BinaryWriter(fs); 
    bw.Write(data);
    bw.Close();

    return true;
}

Client:

string filename = "C:\..\file.abc";
Uploader.Upload up = new Uploader.Upload();
FileStream  fs = File.Create(fileName); 
BinaryReader br = new BinaryReader(fs);

// Read all the bytes
Byte[] data = br.ReadBytes();
up.Write(filename,data);
Manish Sinha
I haven't checked the code. Not sure this works, just wanted to convey the idea. This is nearly what FTP does, except that instead of port 20/21, everything is happening over port 80.
Manish Sinha
A: 

For IIS 6.0 you can change AspMaxEntityAllowed in Metabase.xml, but I don't think it's as straight forward in IIS 5.1.

This link may help, hope it does:

http://itonlinesolutions.com/phpbb3/viewtopic.php?f=3&amp;t=63

adrianos
A: 

Hello,

I think you should use Response.TransmitFile, this method does not load in web server memory the file, it streams the file without using web server resources.

if (Controller.ValidateFileExist())
        {
            ClearFields();
            Response.Clear();
            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "FileNAme.Ext"));
            Response.TransmitFile(FileNAme.Ext);
            Response.End();
            Controller.DeleteFile();
        }

Hope it helps

Regards,

Arturo Caballero
Do not open file in memory, you will take down the server!! With all respect, all other solutions posted seems to work with small files, but not with 1Gig files
Arturo Caballero
This method can be used backwards, for download or upload large files, let me see if I can find the docs
Arturo Caballero