file upload in asp.net

  • Thread starter Thread starter Nicole Legroe
  • Start date Start date
N

Nicole Legroe

I have an asp.NET site running. I want to make it possible in my webpage, to
enter the name of an imagefile on my computer and to upload this file on my
webserver. What is the code to make this upload possible?

Nicole Legroe
(e-mail address removed)
 
Hi Nicole.
Use the input file control, at HTML Controls toolbox.
This control allow you to do images upload.
See bellow the code

* Photo is my input file control.

string fileName = null;
string filePath = null;
string folder = null;
string result = Photo.ImageURL;
folder = Server.MapPath("~/Photos");
fileName = Photo.PostedFile.FileName;
fileName = Path.GetFileName(fileName);
if (Photo.PostedFile.ContentLength != 0)
{
// Create the folder if it does not exist.
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
// Save the uploaded file to the server.
filePath = string.Format("{0}\\{1}", folder,fileName);
if (!File.Exists(filePath))
Photo.PostedFile.SaveAs(filePath);
result = string.Format("~/Photos/{0}", fileName);
}
return result;

Rgs.
Fernando
 
Back
Top