开发者

Upload file without loading to ram in asp.net mvc

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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜