protected void Button1_Click(object sender, System.EventArgs e)
{
// Specify the path on the server to save the uploaded picture to. MUST END WITH A TRAILING SLASH!
string SavePath = Server.MapPath("PictureFolder\\");
if ((FileUpload1.HasFile == true)) {
//Get the correct file extension.
string PictureFileExtension = GetPictureFileType(ref FileUpload1);
if (PictureFileExtension != ".UNKNOWN") {
int fileSize = FileUpload1.PostedFile.ContentLength;
if ((fileSize < 2097152)) {
//Delete any previously uploaded pictures with this name.
System.Collections.ObjectModel.ReadOnlyCollection PictureFiles = My.Computer.FileSystem.GetFiles(SavePath, FileIO.SearchOption.SearchTopLevelOnly, Now.ToString("'Picture'yyyyMMddhhmmssfffffff") + PictureFileExtension);
if (PictureFiles.Count > 0) {
for (ushort P = 0; P <= PictureFiles.Count - 1; P++) {
System.IO.FileInfo FileData = My.Computer.FileSystem.GetFileInfo(PictureFiles.Item(P));
if (FileData.Extension != PictureFileExtension) {
My.Computer.FileSystem.DeleteFile(FileData.FullName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently);
}
}
}
SavePath += Now.ToString("'Picture'yyyyMMddhhmmssfffffff") + PictureFileExtension;
FileUpload1.SaveAs(SavePath);
Response.Write("The picture was uploaded successfully.");
} else {
Response.Write("The picture was not accepted because it exceeds 2 megabytes in size.");
}
} else {
Response.Write("The file was not a picture that can be displayed by this system.");
}
} else {
Response.Write("You did not specify a picture to upload.");
}
Response.Flush();
}
public static string GetPictureFileType(ref FileUpload UploadedFile)
{
string functionReturnValue = null;
if ((UploadedFile.PostedFile.ContentType == null)) {
return ".UNKNOWN";
return functionReturnValue;
}
switch (UploadedFile.PostedFile.ContentType.ToLower) {
case "image/gif":
return ".GIF";
case "image/jpeg":
return ".JPG";
case "image/pjpeg":
return ".JPG";
case "image/x-png":
return ".PNG";
case "image/jpg":
return ".JPG";
case "image/bmp":
return ".BMP";
case "image/x-wmf":
return ".WMF";
default:
return ".UNKNOWN";
}
return functionReturnValue;
}
Upload picture and correct file extension.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment