This project is read-only.

HttpFile content

Topics: faq
Sep 25, 2008 at 7:38 AM
I have decoded the form contents and retrieved a HttpFile from request.Form.getFile("file"). How do I read the data in the file?

Sep 25, 2008 at 7:53 AM
The Filename property of HttpFile contains the full path to the file (where it has been stored on the computer).
It's up to you to do whatever you want with it.

If it's a textfile you can read it as this:
string content = File.ReadAllText(Request.Form.GetFile("myFile").Filename);

To move it (all files that are not moved will be deleted when the request have been handled).

HttpFile file = Request.Form.GetFile("myFile");
File.Move(file.Filename, "C:\\mypath\\" + file.UploadFilename); // upload filename contains the original filename (filename on the users harddrive)

Sep 25, 2008 at 9:46 AM
I see. I was looking for a binary stream to read from.

I suggest that you let HttpFile extend System.IO.File and just add ContentType and UploadFilename. Then it can immediately be used as an ordinary file.

Sep 25, 2008 at 10:05 AM
Sorry, not everyone wants to process the file as a stream, and we support big files to. Let's say that you have 40 concurrent users that all upload files sized 10Mb, having all in streams would make the WebServer use 400Mb memory just for the files.

You can easily create a stream with it:
FileStream stream = new FileStream(Request.Form.GetFile("myFile").Filename, FileMode.Open);
Sep 25, 2008 at 10:41 AM
No need to be sorry, in this particular case it was easier for me when I could File.Move it to the correct place.

And my comment to extend System.IO.File was not very informed. I was thinking it was more like the class, where an object represents a particular file. I see now that it is a namespace for static operations on files.