Click here to Skip to main content
12,894,249 members (72,753 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

21.7K views
22 bookmarked
Posted 15 Apr 2009

File Uploader in ASP.NET and ASP.NET MVC

, 15 Apr 2009
Rate this:
Please Sign up or sign in to vote.
A class to upload files in ASP.NET and ASP.NET MVC.IntroductionUploading 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 validate all the necessities i.e.

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

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 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
UploadPathFull path where file should be uploaded exculding the filename. (Mandatory).
UploadNameThe name which should be used to save the uploaded file with extension. (Optional).
PrefixThe prefix to the filename. (Optional).
SuffixThe suffix to the filename. (Optional).
Conditional Settings
MinSizeThe minimum size of the file in bytes.
MaxSizeThe maximum size of the file in bytes.
IsOverwriteWhether a file is to overwrite or not.
IsEncryptNameWhether a file name is to encrypt or not.
IsLowerNameWhether a file name is to lower or not.
IsRemoveSpaceWhether space is to be removed from a file name or not.
AllowedExtensionsThe allowed file extensions with period.
IsRemoveSpaceThe allowed mime types.
Object Properties
UploadErrorThe object of UploadError class. This class holds error code and error message.
PostedFileThe object of PostedFile class. This class holds different informations of posted file.
UploadedFileThe object of UploadedFile class. This class holds different informations of uploaded file.
Properties of UploadError Class
CodeThe error code.
MessageThe error message.
Properties of PostedFile and UploadedFile Class
FileNameThe file name with extension.
RawNameThe file name excluding extension.
FileExtensionThe file extension with period.
MimeTypeThe file MIME type.
FullPathThe absolute path including the file name.
FilePathThe absolute path to the file excluding the file name.
FileSizeThe size of the file in bytes.
IsImageWhether the file is image or not.

If your upload is not succeded then you can get the error code and message by UploadError property. For example:

strring erroCode    = uploader.UploadError.Code;
string errorMessage = uploader.UploadError.Message;

Remember, here error code is not 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 succeeded, 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!

Downloads

Download Uploader class here. Uploader Class

Download Uploader Sample In ASP.NET here. Uploader Sample In ASP.NET

Download Uploader Sample In ASP.NET MVC here. Uploader Sample In ASP.NET MVC

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

Share

About the Author

Jahedur Rahman Chowdhury
Software Developer (Senior)
Bangladesh Bangladesh
Software Engineer, Bangladesh.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 895346631-May-12 22:34
memberMember 895346631-May-12 22:34 
GeneralMy vote of 1 Pin
Ramon Smits16-Apr-09 1:00
memberRamon Smits16-Apr-09 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 15 Apr 2009
Article Copyright 2009 by Jahedur Rahman Chowdhury
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid