This is a class to upload files in ASP.NET and ASP.NET MVC.
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 can 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 application.
Using this Class
Here, I am giving 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 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 exculding 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 image or not|
If your upload is not succeeded, then you can get the error code and message by
UploadError property. For example:
string erroCode = uploader.UploadError.Code;
string errorMessage = uploader.UploadError.Message;
Remember, here error code is not the actual http error number. I have used numbers from 1 to 10 so that the corresponding message can be customized.
On the other hand, if your upload attempt is succeeded, 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;