'I'm trying to save an image to a folder in asp.net project but it doesn't work

The page refreshers and doesn't stop, and when I exit and check images folder, I find it empty. Plus if I click the upload button multiple times, I get the error

The process cannot access the file 'C:\Users\Setup Game\source\uploads\Kleki.png' because it is being used by another process

How do I fix this?

Here is my code:

if (FileUpload1.PostedFile != null)
{
    string s = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("uploads/" + s));
    Label25.Text = "image saved";
}

I also tried this code :

FileUpload1.SaveAs(Request.PhysicalApplicationPath + "/uploads/" + FileUpload1.FileName.ToString());


Solution 1:[1]

  • Glad that you have cleared the issue.
  • Based on your comments posting that as an answer to help the other community members who are facing with the same issue.
  • Thanks to Steve for the valuable comment.
  • Here is the correct syntax which we need to use to save an image to a folder in asp.net project.
FileUpload1.PostedFile.SaveAs(Path.Combine(Server.MapPath("uploads"), s));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TechieWords