0
votes

I built a web app that allows users to upload a file to a folder. I was given the following exception.

System.UnauthorizedAccessException: Access to the path 'C:\Users\NAME\Documents\Uploads' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)'

I changed the sharing permissions on this file to allow everyone to read and change the file and I continue to run into the exception.

My Code Is Here:

<%@ Page Language="C#" %>
<script runat="server">

    void SubmitButton_Click(Object sender, EventArgs e) {
       if (File1.PostedFile != null) {
          try {
             File1.PostedFile.SaveAs("C:\\Users\\NAME\\Documents\\Uploads");
             Span1.InnerHtml = "Upload Successful!";
          }
          catch (Exception ex) {
             Span1.InnerHtml = "Error saving file <b>C:\\" +
                File1.Value + "</b><br>" + ex.ToString();
          }
       }
    } 

</script>
<html>
<head>
</head>
<body>
    <form runat="server" enctype="multipart/form-data">
        Select a file to upload:<br />
        <input type="file" id="File1" runat="Server">
        <p>
        <input type="submit" id="Submit1" runat="Server" 
         value="Upload File" OnServerClick="SubmitButton_Click">
        <p>
        <span id="Span1" runat="Server" />
    </form>
    </span>
    <p>
</body>
</html>
2
Not real familiar with asp.net so excuse my ignorance, but don't you need to specify a file name along with the path? - TurtlesAllTheWayDown

2 Answers

0
votes

void SubmitButton_Click(Object sender, EventArgs e) {
   if (File1.PostedFile != null) {
      try {

     File1.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("/Uploads"));
         Span1.InnerHtml = "Upload Successful!";
      }
      catch (Exception ex) {
         Span1.InnerHtml = "Error saving file <b>C:\\" +
            File1.Value + "</b><br>" + ex.ToString();
      }
   }
} 

In the SaveAs Method , you need to tell the application to use Server.MapPath method to use the path in your application and not on the absolute path.

0
votes

Check the properties of upload folder, it may be read only checkbox is checked.