This is the first time I am building a web app for the sole purpose of processing user uploaded files and I have a few questions in regards to how this is normally done:
-
Are there any security issues that I have to take into account? The files to be processed are in essence text files that my app will read line by line. Should I limit the file upload extension and/or is there any other precautions I should take into account?
-
What is the best organization method for uploaded files? These files do not need to be stored permanently in my app so should I just dump them in a general “Data” folder and delete whatever is no longer needed?
-
Are there any other important aspects to building web apps with similar functionalities that I’ve missed?
Thanks
The only security issue you have to watch for is inserting the raw text (without data scrubbing to prevent SQL injections) into the database. If there is no database involved, you should be fine. As for extensions, limiting extensions is really a poor top-level filter. It’s good to have, but it’s only peering skin deep into what the file really contains. A file size limit would help also.
Saving to the disk can be costly with a large amount of transactions, but on the other hand, it will clutter your server memory less as more requests/more threads are being used. You can also work with the files in-memory, but for large files, it may end up being detrimental. Consider what you’re working with and choose the best approach.
Define a timeout so that large uploaded files won’t be occupying unnecessary server processes when in the end it’s too large anyway.
I am assuming that you’re working with ASP.NET’s
FileUploadcontrol. Bear in mind that the file does not persist through postbacks (to prevent a security loophole), so the user has to keep browsing to the file each time the page is requested. This is a nuisance if you have server-side validators.Edited to answer comment:
By working in-memory, I am talking about manipulating the file uploaded purely through code without resorting to saving it physically on the server’s disk.
For instance, if you’re using a
FileUploadcontrol, then the user’s file can be accessed through a Stream objectFileUpload.FileContentor as a byte arrayFileUpload.FileBytes(API Reference). Since that’s aStreamyou can just read the file on the fly without having to save it first.Markup:
Codebehind:
See? No need to save to the disk at all.
fileUploadControl.FileBytescontains a bytearray of the data uploaded.If you wanted to save to a file, then you can just use the stream to write to the disk.