Friday, May 28, 2010

Image Uploader

private void UploadLogo(string LogoFileName)
{
if (FileUpload1.HasFile)
{
string fileExt =
System.IO.Path.GetExtension(FileUpload1.FileName);


try
{

if (Directory.Exists(Server.MapPath("..\\AssociateLogos")))
{
//string path = "\\images\\";
string path = "..\\AssociateLogos\\";

int a = LogoFileName.LastIndexOf("/");
string fileName = LogoFileName.Substring(a+1);
string Serverpath = Server.MapPath(path);
FileUpload1.SaveAs(Serverpath +
fileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "
" +
FileUpload1.PostedFile.ContentLength + " kb
" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
else
{
Directory.CreateDirectory(Server.MapPath("..\\AssociateLogos"));
string path = "..\\AssociateLogos\\";
string Serverpath = Server.MapPath(path);
FileUpload1.SaveAs(Serverpath +
LogoFileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "
" +
FileUpload1.PostedFile.ContentLength + " kb
" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}

}
else
{
Label1.Text = "You have not specified a file.";
}
}

No comments:

Post a Comment