Wednesday, February 4, 2009

The FileUpload Control

Well it has been awhile, lots going on. Today I was thumbing around some of the apps that I have created in the last year or so and found this little gem. I have had the need to upload as many as 6 pictures into database and folder locations at the same time before, and the saving grace was the FileUpload Control. It has built in functionality that will allow you to manipulate the file to do pretty much anything you want. I will upload one of my better examples later this evening.


For your code behind you want to do something like:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (imgUpload.HasFile)
try
{
string serverFileName = Path.GetFileName(imgUpload.PostedFile.FileName);
//imgUpload.PostedFile.SaveAs(@"c:\" + serverFileName);
imgUpload.PostedFile.SaveAs(MapPath(".") + serverFileName);
}
catch (Exception err)
{
thorw err
}
}
}

No comments: