Monday, March 5, 2007

Uploading Images to Server

Last week I was working on a project and in web form we wanted to ask people to upload their images to the server, and fill the application form.

So I tried to use the FileUpload Control which is a new WebControl in ASP.NET to upload the images to the server. The point was that I wanted to restrict people to only upload images with file extension "jpg" and "gif", so because the FileUpload control by default allow all types of file format to be uploaded, I used a regular expression to extract the file extension from the file name.
Another important thing for me was the file size of the images. I just want to allow users to upload images with max size of 50Kb, so I used the Length on FileContent property of the FileUpload, and at the end of my code, I used the SaveAs method to save the file in a specific way.

Notice that while your working with web application you have to get the physical path of your web application, not the so I used the Server.MapPath(Request.ApplicationPath) to get the physical path.



Anonymous said...

Please upload again.I cannt download it.

Masoud Tabatabaei said...

Please recheck it.

Anonymous said...

Hi Masoud, you have the code for upload the images... i have a problem i cannot save the destinity path or only the name of the image.... i not like the origen path :)) please you have this code :))

I am very grateful.



My mail is