Uploading files is a common scenario and an essential part of many web applications. Here I have created a class which can be used to upload files easily. The uploader class automatically validates all the necessities i.e. validation of upload directory, file extensions, etc.
There are plenty of resources on uploading a file in ASP.NET. But I have felt the need to have a class which would be easier and customizable as well as perform validation, add prefix and/or suffix to file, encrypt filename, allow choice to overwrite existing file or not and so on. You have to just set the necessary properties you need. You can use it in your ASP.NET as well as ASP.NET MVC applications.
Using this Class
Here I am give an example to use this class:
Uploader uploader = new Uploader();
uploader.UploadPath = Server.MapPath("~\\Content\\Uploads");
uploader.IsLowerName = true;
uploader.IsEncryptName = false;
uploader.Prefix = "Hello_";
uploader.Suffix = "_Boom";
bool success = uploader.DoUpload("MyFile");
MyFile is the name of file input type.
<input type="file" name="MyFile" id="MyFile" />
<asp:FileUpload ID="MyFile" runat="server" />
There is an overload of
DoUpload() method. You can either pass the name of the file input as shown above or
HttpPostedFile file object as follows:
HttpPostedFile postedFile =
HttpContext.Current.Request.Files["MyFile"] as HttpPostedFile;
There is also a method by which you can get the
HttpPostedFile object by supplying the name of the file input.
HttpPostedFile postedFile = uploader.GetHttpPostedFile("MyFile");
|Full path where file should be uploaded excluding the filename(Mandatory)|
|The name which should be used to save the uploaded file with extension (Optional)|
|The prefix to the filename (Optional)|
|The suffix to the filename (Optional)|
|The minimum size of the file in bytes|
|The maximum size of the file in bytes|
|Whether a file is to overwrite or not|
|Whether a file name is to encrypt or not|
|Whether a file name is to lower or not|
|Whether space is to be removed from a file name or not|
|The allowed file extensions with period|
|The allowed mime types|
|The object of |
UploadError class. This class holds error code and error message.
|The object of |
PostedFile class. This class holds different information of posted file.
|The object of |
UploadedFile class. This class holds different information of uploaded file.
|Properties of UploadError Class|
|The error code|
|The error message|
|Properties of PostedFile and UploadedFile Class|
|The file name with extension|
|The file name excluding extension|
|The file extension with period|
|The file MIME type|
|The absolute path including the file name|
|The absolute path to the file excluding the file name|
|The size of the file in bytes|
|Whether the file is an image or not|
If your upload is not successful, then you can get the error code and message with the
UploadError property. For example:
string erroCode = uploader.UploadError.Code;
string errorMessage = uploader.UploadError.Message;
Remember, here the error code is not the actual HTTP error number. I have used numbers from 1 to 10 so that corresponding message can be customized.
On the other hand, if your upload attempt is successful, then you can use information related to the posted file as well as uploaded file by
UploadedFile. For example:
string previousFilename = uploader.PostedFile.FileName;
string previousFilePath = uploader.PostedFile.FilePath;
string uploadedFilename = uploader.UploadedFile.FileName;
string uploadedFilePath = uploader.UploadedFile.FilePath;
You can see it in my blog.
- 23rd January, 2009: Initial version