Share via

ASP.NET: Error saving files into folders using file upload control

mush 181 Reputation points
2023-05-06T05:26:06.35+00:00

I'm trying to save files using fileupload control in asp.net. and i made a condition in which: if the file extension is pdf it won't be deleted from the folder but if it's in png format it will be deleted.

I'm getting the following error after clicking the submit button to save them in the database:

"Could not find file {'C:\Users\user\source\repos\project1\En\files\Drawing1.pdf'.":"C:\Users\user\source\repos\projec\En\files\Drawing1.pdf"}

this is my vb.net:

Does anyone know what is the possible reason?thanks

'passport
            If fuFamilyPassportUploadFile.HasFile = True Then
                Dim filen As String
                filen = Path.GetFileName(fuFamilyPassportUploadFile.FileName)
                Dim FileExtension As String = Path.GetExtension(fuFamilyPassportUploadFile.FileName)
                If FileExtension.ToUpper <> ".PDF" Then
                    Dim fs As FileStream = New FileStream(Server.MapPath("~/En/files/" & filen), FileMode.Open, FileAccess.Read)
                    Dim br As BinaryReader = New BinaryReader(fs)
                    Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
                    br.Close()
                    fs.Close()

                    InscmdFamilyDetails.Parameters.AddWithValue("@PassportImage", SqlDbType.Image).Value = bytes
                    InscmdFamilyDetails.Parameters.AddWithValue("@PassportFile", SqlDbType.NVarChar).Value = filen

                    If System.IO.File.Exists(Server.MapPath("~/En/files/" & filen)) = True Then
                        System.IO.File.Delete(Server.MapPath("~/En/files/" & filen))
                    End If
                Else
                    Dim fs As FileStream = New FileStream(Server.MapPath("~/En/files/" & filen), FileMode.Open, FileAccess.Read)
                    Dim br As BinaryReader = New BinaryReader(fs)
                    Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
                    br.Close()
                    fs.Close()

                    InscmdFamilyDetails.Parameters.AddWithValue("@PassportImage", SqlDbType.Image).Value = bytes
                    InscmdFamilyDetails.Parameters.AddWithValue("@PassportFile", SqlDbType.NVarChar).Value = filen



                End If

            End If
Developer technologies | VB
Developer technologies | ASP.NET Core | Other

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,221 Reputation points Microsoft External Staff
    2023-05-08T06:46:14.3433333+00:00

    Hi @mush,

    You're getting Server.MapPath(Server.MapPath specifies the relative or virtual path to map to a physical directory), but I can tell from the code that you're not saving the uploaded file to Server.MapPath, so the file won't be found.

    fuFamilyPassportUploadFile.SaveAs(Server.MapPath("~/En/files/" & filen))

     If fuFamilyPassportUploadFile.HasFile = True Then
                Dim filen As String
                filen = Path.GetFileName(fuFamilyPassportUploadFile.FileName)
                Dim FileExtension As String = Path.GetExtension(fuFamilyPassportUploadFile.FileName)
                'Save File to Server.MapPath
                fuFamilyPassportUploadFile.SaveAs(Server.MapPath("~/En/files/" & filen))
                If FileExtension.ToUpper <> ".PDF" Then
    
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.