Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fileupload saveas method doesn't overwrite

Tags:

asp.net

I have a very simple requirement, there's a folder containing an image file, I have a form with only one upload field to select an image and save it with the same existing image name to overwrite it

protected void ChangeLogo(object sender, EventArgs e)
    {   
        if (!ImageUpload.HasFile)
        {
            ShowPopup("Logo Upload Canceled", "Please upload the image for the logo.", "stop");
        }            
        else //save the image
        {
            string logoPath = Server.MapPath("~/images/home/");
            string filename = "logo.png";

            ImageUpload.SaveAs(logoPath + filename);
        }
    }

I am getting an error: Access to the path 'C:\inetpub\wwwroot\website\images\home\logo.png' is denied even though there's full access control on the folder but if i saved it with a different name it works, it only refuses to overwrite and I need to overwrite. I thought of first deleting the image then saving, but this is silly, why can't I overwrite?

Thanks in advance

like image 844
Yasmine Avatar asked May 22 '26 19:05

Yasmine


1 Answers

Naive solution:

If(File.Exists(logoPath + filename))
    File.Delete(logoPath + filename);
ImageUpload.SaveAs(logoPath + filename);
like image 172
Icarus Avatar answered May 24 '26 17:05

Icarus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!