Bashar Kokash' Blog

.Net Framework, windows and web development.

This site

Resources

Friends

Sponsors

  • MaximumASP
  • Packet Sniffer
    Home Loan
  • optimum rewards

Uploading files in ASP.NET

 In this post I’ll show how to upload files to a server directory, and discuss several used techniques step by step.

In ASP.NET, files can be uploaded many ways. One way is to use the FileUpload control because it’s easy and ready to use.

Some issues should be considered:

1.       The types of the file to be uploaded; images (jpg, gif, png) or sound files (wav, rm, mp3…).

2.       Where to save the files; copy the file to the server’s file system and save the file path in a database table or we can store the files directly in the database as bytes.

3.       And some more else as I go through details.

 

Add FileUpload control, a Button and a Label to the web page

FileUpload 

Starting with file extensions:

First, only allowed file extensions are stored to an array of strings. In case of uploading image files, the common image file extensions are: .jpg, .gif, and .png etc...

You can add whatever you want

string[] ImageExtentions ={ ".jpeg", ".gif", ".png", ".jpg" };

//Get the file extension of the file in the FileUpload control
string FileExtention = Path.GetExtension(FileUpload1.FileName).ToLower();

 

If the “Browse” button of the FileUpload was pressed and a file was chosen then the Boolean HasFile Property of the FileUpload is set to true. So we proceed only if the HasFile Property is true.

if (FileUpload1.HasFile)

//Test whether the file is of the allowed extensions or not using a simple loop.

bool IsReady = false;

for (int i = 0; i < AllowedExtentions.Length; i++)

{

if (FileExtention == AllowedExtentions[i])

{

IsReady = true;

            Break;

}

}

 

Finally to save the file to the server, it’s easy with a single line of code:

//~represents the root folder

const string Directory = "~/Images/";

FileUpload1.SaveAs(Server.MapPath(Directory + FileUpload1.FileName));

 

Then the file path should be stored in the datadbase to be retrived later.

 

 

But what if the file name is already exist in the server folder, the previous instruction will overwrite the file, so we may face a problem of loosing files of the same names but different contents. To solve this problem two different solutions can be used:

 

We can simply change the name as follows:

 

string FilePath = Directory + FileUpload1.FileName;

string FileName = Path.GetFileNameWithoutExtension(FileUpload1.FileName);

string fileExtention = Path.GetExtension(FileUpload1.FileName);

int iteration = 1;

while (File.Exists(Server.MapPath(FilePath)))

{

FilePath = string.Concat(Directory, FileName, "", iteration, fileExtention);

iteration++;

}

try

{

FileUpload1.SaveAs(Server.MapPath(FilePath));

}

catch (Exception ex)

{ 

UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;

}

 

For example if the file name is apple.gif and the file is already exist this will try the name apple1.gif, apple2.gif and so on.

 

Or we could get a random generated file name

 

string temp=Path.GetRandomFileName();

string FileName = temp.Remove(temp.LastIndexOf('.'));

string fileExtention = Path.GetExtension(FileUpload1.FileName);

string FilePath = Directory+ FileName+ fileExtention;

try

{

FileUpload1.SaveAs(Server.MapPath(FilePath));

}

catch (Exception ex)

{

UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;

}

 

UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;

}

 

For example if the file name is apple.gif and the file is already exist this will try the name apple1.gif, apple2.gif and so on.

 

Or we could get a random generated file name

 

string temp=Path.GetRandomFileName();

string FileName = temp.Remove(temp.LastIndexOf('.'));

string fileExtention = Path.GetExtension(FileUpload1.FileName);

string FilePath = Directory+ FileName+ fileExtention;

try

{

FileUpload1.SaveAs(Server.MapPath(FilePath));

}

catch (Exception ex)

{

UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;

}

 

Posted: Sep 11 2007, 10:35 PM by BasharKokash | with 1 comment(s)
Filed under: ,

Comments

mikael said:

Thanks man!!! This is quite enlightening. More power guys!!!You rock people!

# February 12, 2008 12:59 AM
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.