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
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,385 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,636 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,121 Reputation points Microsoft Vendor
    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.

    0 comments No comments