Skip to content

Instantly share code, notes, and snippets.

@Ryanb58
Created July 21, 2014 18:50
Show Gist options
  • Save Ryanb58/645b8229c4f478a63464 to your computer and use it in GitHub Desktop.
Save Ryanb58/645b8229c4f478a63464 to your computer and use it in GitHub Desktop.
Asp.net C# Windows Forms Upload Image
// logoUpload is the name of the .net upload control.
string sSavePath = "/images/";
if (logoUpload.PostedFile != null)
{
HttpPostedFile myLogo = logoUpload.PostedFile;
//Check Length.
int nFileLen = myLogo.ContentLength;
if (nFileLen == 0)
{
lblOutput.Text = "No file was uploaded.";
return;
}
//Check file extension/type.
if (System.IO.Path.GetExtension(myLogo.FileName).ToLower() != ".png")
{
lblOutput.Text = "Please make sure your logo is a PNG.";
return;
}
//Read file into a stream.
byte[] myLogoData = new Byte[nFileLen];
myLogo.InputStream.Read(myLogoData, 0, nFileLen);
//Get file name.
string sFileName = System.IO.Path.GetFileName(myLogo.FileName);
//Save new image to disk.
FileStream newFile = new FileStream(Server.MapPath(sSavePath + sFileName), FileMode.Create);
newFile.Write(myLogoData, 0, myLogoData.Length);
newFile.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment