Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Uploader in ASP.NET and ASP.NET MVC

0.00/5 (No votes)
27 Jan 2009 3  
A class to upload files in ASP.NET and ASP.NET MVC

Introduction

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.

Background

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";

    uploader.AllowedExtensions.Add(".jpg");
    uploader.AllowedExtensions.Add(".jpeg");
    uploader.AllowedExtensions.Add(".gif");
    uploader.AllowedExtensions.Add(".png");

    bool success = uploader.DoUpload("MyFile");

    if (success)
    {
        //TODO: Code to handle success
    }
    else
    {
        //TODO: Code to handle failure
    }

Here MyFile is the name of file input type.

 <!-- In Html -->
<input type="file" name="MyFile" id="MyFile" />
<%--In tradition ASP.NET --%>
<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;
uploader.DoUpload(postedFile);

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");
uploader.DoUpload(postedFile);

Configuration Settings

General Settings
UploadPath Full path where file should be uploaded excluding the filename(Mandatory)
UploadName The name which should be used to save the uploaded file with extension (Optional)
Prefix The prefix to the filename (Optional)
Suffix The suffix to the filename (Optional)
Conditional Settings
MinSize The minimum size of the file in bytes
MaxSize The maximum size of the file in bytes
IsOverwrite Whether a file is to overwrite or not
IsEncryptName Whether a file name is to encrypt or not
IsLowerName Whether a file name is to lower or not
IsRemoveSpace Whether space is to be removed from a file name or not
AllowedExtensions The allowed file extensions with period
IsRemoveSpace The allowed mime types
Object Properties
UploadError The object of UploadError class. This class holds error code and error message.
PostedFile The object of PostedFile class. This class holds different information of posted file.
UploadedFile The object of UploadedFile class. This class holds different information of uploaded file.
Properties of UploadError Class
Code The error code
Message The error message
Properties of PostedFile and UploadedFile Class
FileName The file name with extension
RawName The file name excluding extension
FileExtension The file extension with period
MimeType The file MIME type
FullPath The absolute path including the file name
FilePath The absolute path to the file excluding the file name
FileSize The size of the file in bytes
IsImage 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 PostedFile and UploadedFile. For example:

string previousFilename = uploader.PostedFile.FileName;
string previousFilePath = uploader.PostedFile.FilePath;

string uploadedFilename = uploader.UploadedFile.FileName;
string uploadedFilePath = uploader.UploadedFile.FilePath;

Enjoy this!

Conclusion

You can see it in my blog.

History

  • 23rd January, 2009: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here