I’m using jQuery to upload an image and it has worked fine until now.
The image gets saved but it get saved TWICE with two different filenames. The filename is generated from DateTime.Now. The upload control lies within a form which is posted to an iframe.
The code for saving the image looks like this:
int SaveInt = 0;
using (var m = new MemoryStream())
{
bmp.Save(m, ImageFormat.Png);
var img = Image.FromStream(m);
var ratio = (double)1100 / img.Width;
var newWidth = (int)(img.Width * ratio);
var newHeight = (int)(img.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(img, 0, 0, newWidth, newHeight);
}
img = Bloggdesign.Tools.CropImage(newImage, new Rectangle(0, 0, 1100,
500));
if (SaveInt == 0)
{
img.Save(Server.MapPath("~/trashcan/") + NewFilename + ".png");
SaveInt++;
}
}
if (FileToDel != null)
{
File.Delete(Server.MapPath("/trashcan/" + FileToDel + ".png"));
}
The SaveInt variable is just for testing but even with the “if (SaveInt ==0” and the added number it still saves the image twice. When I debugged I noticed that the first image gets saved directly after the row where img.Save() is fired and the second image gets saved when it reaches the last “}” for Page_Load. After this the code jumps back up some rows to this:
if (FileToDel != null)
{
File.Delete(Server.MapPath("/trashcan/" + FileToDel + ".png"));
}
And it’s here the second image gets saved. Am I missing something in the code? It just so weird that the image suddenly gets saved twice.
It sounds like you are triggering the code twice. The code looks fine. Can you post the code the is triggering your save and ensure that you are not doing it on the
Page_Loadas well.