0

As an unexperienced developer I have no idea whether this question is phrased properly (I did check the internet but I havent found a solution that worked for me that is why I signed up here). I am trying to make a form where users can upload files to attach to their form (in SharePoint 2013) and I used the following code as an example. The idea is to temporary accept the files and display them to the user and upload them to the document library when the form is submitted. In my code however, this results in "acces denied" and when I debugged it, the following piece of my code seemed to be causing the problem:

public void BtnAttachmentUpload_Click(object sender, EventArgs e)
    {        
        fileName = System.IO.Path.GetFileName(FileUpload.PostedFile.FileName);
        if (fileName != "")
        {
            string _fileTime = DateTime.Now.ToFileTime().ToString();
            string _fileorgPath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
            string _newfilePath = _fileTime + "~" + fileName;
            length = (FileUpload.PostedFile.InputStream.Length) / 1024;
            string tempFolder = Environment.GetEnvironmentVariable("TEMP");
            string _filepath = tempFolder + "\\" + _newfilePath;
            FileUpload.PostedFile.SaveAs(_filepath);

            AddRow(fileName, _filepath, DocNo, true);
            DocNo = DocNo + 1;
            Label.Text = "Successfully added in list";
        }
    }

The last line of the first section(FileUpload.PostedFile.SaveAs(_filepath);) is where it gives the following error:

"System.UnauthorizedAccessException: 'Access to the path 'C:\Users\Spapps\AppData\Local\Temp\131613662837501509~testdoc2.pdf' is denied.' "

Is this a known issue and is there a solution that can help me out?

FlorisdB
  • 1
  • 5

2 Answers2

0

Try with spsecurity.runwithelevatedprivileges

nixcom
  • 26
  • 2
0

I've been looking to close this question but didnt find how to so Ill state it here as an answer. I havent found a solution, but I worked around this issue in my project by altering the approach; I do not temporarily upload and display the files anymore. They are deleted from the doclib if the procedure is cancelled.

FlorisdB
  • 1
  • 5