I am trying to create a utility in C# using the MVC framework where a user uploads a picture which is used to crop pieces out of to be used as thumbnail icons (only one user at a time will be doing this at any given time). The user can then upload a different picture and continue cropping.
I have a controller action that handles the file upload taking the picture, converting it from whatever format it's in to a jpeg and saving it as "temp.jpg" (I know it's not a thin controller, but this is just for testing purposes). When they upload the next picture I want to replace that temp.jpg with this new image. This controller works fine in development on my machine, but in production after the user uploads the first image and tries to replace it with another image they get the following error:
"the process cannot access the file because it is being used by another process"
It seems to me the the "temp.jpg" file is being locked after the first upload and I can't figure out how to avoid this.
Any suggestions or alternative ideas are welcome.
Break down of what my code does:
- Checks if the picture to be uploaded exists on the server and deletes it if found
- Saves the picture as is with it's original file name and extension
- Checks for the "temp.jpg" file and deletes it if found
- Opens the original image in a System.Drawing.Image object to convert to jpeg
- Save it as a new "temp.jpg" to replace the deleted file.
My Code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PicUpload(DateTime sd, FormCollection collection)
{
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory + "Content\\AdContent\\",
Path.GetFileName(hpf.FileName));
FileInfo temp = new FileInfo(savedFileName);
if (temp.Exists) temp.Delete();
hpf.SaveAs(savedFileName);
string tempFileName = AppDomain.CurrentDomain.BaseDirectory + "Content\\AdContent\\temp.jpg";
temp = new FileInfo(tempFileName);
if (temp.Exists) temp.Delete();
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
Image newPic = Image.FromFile(savedFileName);
newPic.Save(tempFileName, encoders[1], codecParams);
newPic.Dispose();
FileInfo tmp = new FileInfo(savedFileName);
if (tmp.Exists) tmp.Delete();
return RedirectToAction("Create", new { startdate = String.Format("{0:MM-dd-yyyy}", sd) });
}
return RedirectToAction("Create", new { startdate = String.Format("{0:MM-dd-yyyy}", sd) });
}