I have a page currently setup to upload 1 image at a time, it uploaded the image, then creates a thumbnail for that image in the same directory. Client needs / wants to be able to upload a few more images at the same time. So i would like to see if i can
use the existing code that works, but setup to upload a few more images and process them all to create the thumbnails.
I currently have a page that allows them to upload 5 images at a time and that works fine, but its just simple uploading the images, nothing else is happening at that moment.
Here is the code i am working with:
protected void UploadBtn_Click(Object sender, EventArgs e)
{
String pth = Server.MapPath("~/gallery/homebirth/images/");
String UploadedFile = MyFile.PostedFile.FileName;
int ExtractPos = UploadedFile.LastIndexOf("/") + 1;
//to retrieve only Filename from the complete path
String UploadedFileName = UploadedFile.Substring(ExtractPos, UploadedFile.Length - ExtractPos);
// Display information about posted file. Div is invisible by default
FileName.InnerHtml = UploadedFileName;
MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString();
//div is made visible
FileDetails.Visible = true;
// Save uploaded file to server at the in the Pics folder
MyFile.PostedFile.SaveAs(pth + UploadedFileName);
//thumbnail creation starts
try
{
//Read in the image filename whose thumbnail has to be created
String imageUrl = UploadedFileName;
//You may even specify a standard thumbnail size
int imageWidth = 120;
int imageHeight = 120;
if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0)
{
Response.End();
}
//the uploaded image will be stored in the Pics folder. to get resize the image, the original image has to be accessed from the Pics folder
imageUrl = pth + imageUrl;
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);
//We need to create a unique filename for each generated image
DateTime MyDate = DateTime.Now;
String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png";
//Save the thumbnail in Png format. You may change it to a diff format with the ImageFormat property
thumbNailImg.Save(pth + MyString, ImageFormat.Png);
thumbNailImg.Dispose();
//Display the original & the newly generated thumbnail
Image1.AlternateText = "Original image";
Image1.ImageUrl = @"~/gallery/homebirth/images/" + UploadedFileName;
Image2.AlternateText = "Thumbnail";
Image2.ImageUrl = @"~/gallery/homebirth/images/" + MyString;
}
catch (Exception ex)
{
Response.Write("An error occurred - " + ex.ToString());
}
}
Intermediate ASP.net User, Using VS2008/VS2010 with C# and SQL2005, SQL2008, Silverlight 3
---------------------
Mark as Answered if it helped
cubangt
Contributor
3051 Points
2388 Posts
How to change this code to allow more file uploads
Aug 28, 2010 03:41 AM|LINK
I have a page currently setup to upload 1 image at a time, it uploaded the image, then creates a thumbnail for that image in the same directory. Client needs / wants to be able to upload a few more images at the same time. So i would like to see if i can use the existing code that works, but setup to upload a few more images and process them all to create the thumbnails.
I currently have a page that allows them to upload 5 images at a time and that works fine, but its just simple uploading the images, nothing else is happening at that moment.
Here is the code i am working with:
protected void UploadBtn_Click(Object sender, EventArgs e) { String pth = Server.MapPath("~/gallery/homebirth/images/"); String UploadedFile = MyFile.PostedFile.FileName; int ExtractPos = UploadedFile.LastIndexOf("/") + 1; //to retrieve only Filename from the complete path String UploadedFileName = UploadedFile.Substring(ExtractPos, UploadedFile.Length - ExtractPos); // Display information about posted file. Div is invisible by default FileName.InnerHtml = UploadedFileName; MyContentType.InnerHtml = MyFile.PostedFile.ContentType; ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString(); //div is made visible FileDetails.Visible = true; // Save uploaded file to server at the in the Pics folder MyFile.PostedFile.SaveAs(pth + UploadedFileName); //thumbnail creation starts try { //Read in the image filename whose thumbnail has to be created String imageUrl = UploadedFileName; //You may even specify a standard thumbnail size int imageWidth = 120; int imageHeight = 120; if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0) { Response.End(); } //the uploaded image will be stored in the Pics folder. to get resize the image, the original image has to be accessed from the Pics folder imageUrl = pth + imageUrl; System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl); System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero); //We need to create a unique filename for each generated image DateTime MyDate = DateTime.Now; String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png"; //Save the thumbnail in Png format. You may change it to a diff format with the ImageFormat property thumbNailImg.Save(pth + MyString, ImageFormat.Png); thumbNailImg.Dispose(); //Display the original & the newly generated thumbnail Image1.AlternateText = "Original image"; Image1.ImageUrl = @"~/gallery/homebirth/images/" + UploadedFileName; Image2.AlternateText = "Thumbnail"; Image2.ImageUrl = @"~/gallery/homebirth/images/" + MyString; } catch (Exception ex) { Response.Write("An error occurred - " + ex.ToString()); } }---------------------
Mark as Answered if it helped