Waleed Baig
Waleed Baig

Reputation: 422

The process cannot access the file '\image_o_6.jpg' because it is being used by another process

I have a scenario and I am stuck in it for 2 days .

When i upload an image HttpPostedFileBase it saves successfully after that when I update it, it gives me the error of access denied

my method is as follow:

public void UploadImage(HttpPostedFileBase image, int imageID)
{
    var origionalFileName = "image_o_" + imageID + Path.GetExtension(image.FileName);
    var imagePath = HttpContext.Current.Server.MapPath("~/images/" + (int)eDirectory.Brands + "/" + imageID % 10 + "/");

    // ---- Save original
     if (File.Exists(Path.Combine(imagePath, origionalFileName)))
    {
        File.Delete(Path.Combine(imagePath, origionalFileName));
    }
    else
      image.SaveAs(Path.Combine(imagePath, origionalFileName));
    // ---- Save resized images
    foreach (var size in Sizes)
    {
        var fileName = "image_" + size + "_" + imageID + Path.GetExtension(image.FileName);

        // ---- Resize
        var buffer = GetResizedImage(Path.Combine(imagePath, origionalFileName), size, size);

        var path = Path.Combine(imagePath, fileName);
        File.WriteAllBytes(path.ToString(), buffer);
    }
}

KeyNotes:

The new image I have to upload must have the same name .

Error

The process cannot access the file '\image_o_6.jpg' because it is being used by another process.

Is there anything wrong am i doing ?

Upvotes: 0

Views: 644

Answers (0)

Related Questions