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