views:

250

answers:

2

Hi, i've notice that this was not asked before so i want to ask this question...

currently im writing an upload controller to upload some quite a large files. but i notice when it upload large file, the CPU and RAM go peak and this is really not what i want to (of course you too)

my controller is look like:

int Count = 0;
        string _uploadsFolder = HostingEnvironment.MapPath("~/Upload/Files/Images/");

        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFileBase _file = Request.Files[i];

            // Validation ......
            FileMIME mimetype = new FileMIME();
            mimetype.AllowedMimeTypes.Add("image/png");
            mimetype.AllowedMimeTypes.Add("image/jpeg");
            mimetype.AllowedMimeTypes.Add("image/gif");

            if ((_file.ContentLength > 0) && (_file.ContentLength < 4194304) && (mimetype.IsImage(_file)))
            {
                string guid = Guid.NewGuid().ToString();
                string _fileLocation = Path.Combine(_uploadsFolder, Path.GetFileName(_file.FileName));

                _file.SaveAs(_fileLocation);

                Count++;
            }
        }

Is there anyway to make this buffer into HDD instead of ram?

thank you very much...

A: 

I don't know for sure, but how would it get to the hard drive without using CPU and RAM? Seems like the server would need to process the stream via RAM in order to write it to the hard drive.

Dennis Palmer
like buffer into Hardisk instead of RAM, currently, the program load everything (1GB file) into ram and then it save to HDD afterward. sometimes my MVC app lock up
DucDigital
A: 

If this coming from a web page then to get it from say the client computer to your server you are going to need to load the stream into memory. I don't think there is a way around this.

There might be ways to mitigate the load on the server. You might be able to do it async leaving the user relatively free to continue.

you may be able to process the data stream in chunks rather and all at once.

There might be others but all these are still going to use ram.

Looks like you may be accepting images. You can resize the image in memory before saving to the database which will reduce any future load on RAM once you have the file but again, this uses RAM when you upload the file.

The (only) other way, and one which is pretty bizare, might be to allow users to ftp the file to your site and you then reference the file with an entry in a table.

You may even be able to do this via your web app though i've never tried.

griegs
I think to do the async it's not realy posible (base on my understanding) because the RAM and CPU go peak as user UPLOADING data to my Server, so if user change page, ofcoure the operation is canceled, isn't it?
DucDigital
Yeah like I said I don't think this is actually going to fix your issue. I just don't think there is any way around this @DucDigital. The fact that you are transfering a file from once computer to another means that you need to use a stream of some sort and that is going to use ram. restrict the size of files would be my recommendation. that way the ram usage won't be as big and the processing time will also be reduced.
griegs
but atlease i saw some paid asembly in .net for handling these thing i think .net is capable of buffer the uploading file to disk, just that currently i dont know how :)
DucDigital