Created
July 21, 2014 18:50
-
-
Save Ryanb58/645b8229c4f478a63464 to your computer and use it in GitHub Desktop.
Asp.net C# Windows Forms Upload Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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