0

this is so that when I need to upload a right an image to an article so it must delete the old image

and it must be clear in the folder where the image is located,

string Tid = DateTime.Now.Ticks.ToString();
        string unikID = Guid.NewGuid().ToString();
        string url = "~/img/bimg/";


        cmd.CommandText = "SELECT img FROM aktiviteter WHERE Id = @id;";
        cmd.Parameters.AddWithValue("@Id", id);

        conn.Open();
        SqlDataReader readerImg = cmd.ExecuteReader();
        if (readerImg.Read())
        {
            File.Delete(Server.MapPath(url.Remove(0, 1) + readerImg["img"]));
        }

        conn.Close();

problem is that I need to find the image in the column in the database where the entity who should delete the picture

Access to the path 'C:\Users\198407\Documents\Visual Studio 2013\WebSites\Jesper-mm-CRUD\img\bimg\' is denied.

my pictures located here: /img/bimg/hello.png

how it looks when I load to the server

string Tid = DateTime.Now.Ticks.ToString();
        string unikID = Guid.NewGuid().ToString();
        string url = "~/img/bimg/";


        cmd.CommandText = "SELECT img FROM aktiviteter WHERE Id = @id;";
        cmd.Parameters.AddWithValue("@Id", id);

        conn.Open();
        SqlDataReader readerImg = cmd.ExecuteReader();
        if (readerImg.Read())
        {
            File.Delete(Server.MapPath(url.Remove(0, 1) + readerImg["img"]));
        }

        conn.Close();

        ImageNet.FluentImage img = ImageNet.FluentImage.FromStream(FileUploadImg.FileContent);
        img.Resize.Scale(360).Save(Server.MapPath(url + unikID + ".png"));


        if (File.Exists(Server.MapPath(url + unikID + ".png")))
        {
            cmd.CommandText = "UPDATE aktiviteter SET navn = @navn, sted = @sted, indhold = @indhold, img = @img, rubrik = @rubrik, retbrugerID = @retbrugerid WHERE Id = @id;";
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Parameters.AddWithValue("@navn", navn);
            cmd.Parameters.AddWithValue("@sted", Sted);
            cmd.Parameters.AddWithValue("@indhold", Indhold);
            cmd.Parameters.AddWithValue("@img", unikID);
            cmd.Parameters.AddWithValue("@rubrik", rubrik);
            cmd.Parameters.AddWithValue("@retbrugerid", brugerid);
        }
  • The error is clearly saying there's some sort of permission issue. When you say "server", are you referring to IIS? If so, make sure your Application Pool identity has read/write access to the path. – Arian Motamedi Nov 24 '14 at 16:10

1 Answers1

1

Since you are getting this error -

Access to the path 'C:\Users\198407\Documents\Visual Studio 2013\WebSites\Jesper-mm-CRUD\img\bimg\' is denied.

I would suggest , granting read/write access to this user = IIS_IUSRS for required directory.

Read more helpful link

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48