سؤال

I'm trying to save a file that I upload from my page but I'm getting an access denied error:

public void SaveFile(Item item, HttpPostedFileBase file)
    {
        var dir = string.Format(@"{0}\NSN\{1}", ConfigurationManager.AppSettings["ContentLocation"].ToString(), item.Id.ToString());

        if (!System.IO.Directory.Exists(dir))
            System.IO.Directory.CreateDirectory(dir);

        Array.ForEach(Directory.GetFiles(dir), File.Delete);

        file.SaveAs(dir);
    }

I'm running this site from the local host from visual studio so no app pool is involved. I've given the Network Service (and Everyone as a test) full control of the folder and it's subfolders. Strange thing is it creates the folder if it needs to and also deletes any files in an existing folder. Only when I call the SaveAs function do I get the error.

هل كانت مفيدة؟

المحلول

You call file.SaveAs with path to directory instead of path to file

نصائح أخرى

Here, give this a try:

string saveAsPath = Path.Combine(dir, file);
file.SaveAs(saveAsPath);

Replace file.SaveAs(dir) with the above.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top