I am having trouble uploading any file to the path in the error message below. Does anyone have any idea what the problem is. I did some research on this and most sites say that the permissions need Read/Write access for the asp.net user. I tried this and I still receiver the error. Here is the error:

For example Error saving file education_calendar.pdf System.IO.DirectoryNotFoundException: Could not find a part of the path "d:\inetpub\aopha\Uploads\". at System.IO.__Error.

Here is my code for the click event. I am not sure what is happening here:

Private Sub cmdUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
        If IsPostBack Then
            Dim MyFileColl As HttpFileCollection = Request.Files
            Dim MyPostedMember As HttpPostedFile = MyFileColl.Get("File1")
            Dim MyFileName As String = MyPostedMember.FileName

            If Not MyPostedMember Is Nothing Then
                Try
                    Dim intFileNameLength As Integer = InStr(1, StrReverse(MyFileName), "\")
                    Dim strFileNameOnly As String = Mid(MyFileName, (Len(MyFileName) - intFileNameLength) + 2)
                    Dim RootPath As String = Server.MapPath("/")

                    'APPEND THE APPROPRIATE FOLDER
                    Dim tmpFolder As String = FileLoc.SelectedValue 'HttpContext.Current.Request.Form("FileLoc")
                    Dim WebFilePath As String = Server.MapPath(tmpFolder & strFileNameOnly) 'HttpContext.Current.Request.MapPath(tmpFolder & strFileNameOnly)

                    MyPostedMember.SaveAs(WebFilePath.ToString)

                    Dim LinkPath As String = WebFilePath.Replace(RootPath, "")
                    LinkPath = "/" & LinkPath.Replace("\", "/")

                    lblMessage.Text = "Successfully saved file: <a href=""" & LinkPath & """ target=""_blank"">" _
                    & tmpFolder & strFileNameOnly & "</a>"

                    'return the file to the opening window.

                    Dim strScript As String = "<script>window.opener.document.forms(0)." + control.Value + ".value = '"

                    If Request.QueryString("retPath") = "link" Then
                        strScript += LinkPath
                    Else
                        strScript += strFileNameOnly
                    End If

                    strScript += "';self.close()"
                    strScript += "</" + "script>"
                    RegisterClientScriptBlock("anything", strScript)
                Catch exc As Exception
                    lblMessage.Text = "Error saving file <b>" _
                                   & File1.Value & "</b><br>" & exc.ToString()
                End Try
            End If
        End If
    End Sub

Dani AI

Generated

This exception means the runtime could not resolve part of the physical path being written to — it is usually a missing or malformed folder path, not a straight permission denial (permission failures usually raise UnauthorizedAccessException). In the posted code the most likely causes are a failed filename extraction (resulting in an empty filename so the app tries to write to a folder) or building the physical path incorrectly with Server.MapPath.

Two quick, reliable fixes to apply:

  • Extract the filename with System.IO.Path.GetFileName instead of string-reverse logic; that handles both full client paths and plain filenames.
  • Resolve the upload folder and then combine paths with System.IO.Path.Combine. Verify the folder exists and create it if necessary. As suggested, log or display the resolved physical path before saving to confirm what is actually being written to.

Example (VB.NET) that addresses both issues:

Dim physFolder As String = Server.MapPath(tmpFolder)          ' tmpFolder = "/Uploads/" or "~/Uploads/"
Dim safeName As String = System.IO.Path.GetFileName(MyPostedMember.FileName)

If String.IsNullOrEmpty(safeName) Then
  Throw New ArgumentException("Uploaded file name is empty")
End If

If Not System.IO.Directory.Exists(physFolder) Then
  System.IO.Directory.CreateDirectory(physFolder)
End If

Dim destination As String = System.IO.Path.Combine(physFolder, safeName)

Then call the uploader save method with the destination path (and log destination first). Additional notes: confirm the IIS process identity (application pool identity) has Modify rights on the physical folder (grant temporarily to Everyone to test if needed), sanitize the filename to remove invalid characters, and consider prefixing with a GUID or timestamp to avoid collisions. Mentioning : the DirectoryNotFoundException indicates the folder part of the path is missing or the final filename string is empty — fixing filename extraction and path composition will usually resolve this.

Recommended Answers

All 4 Replies

Help. Anyone seen or had experience with this error.

The exception is of type DirectoryNotFoundException. So check before upload if the particular directory exist.

Also are you using asp.net Upload Control or share your markup as well.
You code looks more complex than it should to do a file upload.

The exception is of type DirectoryNotFoundException. So check before upload if the particular directory exist.

Also are you using asp.net Upload Control or share your markup as well.
You code looks more complex than it should to do a file upload.

I am not using Upload Control. At least I don't think. How would I know if I am using that? Is that another file or something? That directory does exist.

ok sorry looks you are using HtmlInputFile control.
To me it looks WebFilePath.ToString is not the proper path:

To degub try this and see the path displayed is correct:

//MyPostedMember.SaveAs(WebFilePath.ToString)
Response.Write(WebFilePath.ToString)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.