简体   繁体   中英

ASP.NET/VB.NET FileUpload Control

I have a problem with FileUpload, when I select a file from the local machine, it will not bring the real path of the file, it will use the path for the project files and assume the file I am selecting is there, any ideas?

Example: File name is "Q.JPG" and is in "C:\" when I browse to "C:\" and select "Q.JPG" and click open, I get the following Error Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\q.jpg'. So when I fire up the code for Uploading the file to FTP for example, it will return an error because file doesn't exist

HTML side:

<asp:FileUpload ID="FU" runat="server" Height="24px" />

Below is the VB code:

Protected Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click

    If FU.PostedFile IsNot Nothing AndAlso FU.PostedFile.FileName <> "" Then
        Dim MaxSize As Integer = FU.PostedFile.ContentLength
        If MaxSize > "2097152" Then
            lblUpload.Text = "The file size cannot exceed 2 MB"
            btnSave.Focus()
            GoTo 99
        End If


        '--------------------------
        ' set up request...
        Dim LocFile As String = FU.PostedFile.FileName
        Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://myftp.com/" & LocFile), System.Net.FtpWebRequest)
        clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
        clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        ' read in file...
        Dim bFile() As Byte = System.IO.File.ReadAllBytes(FU.PostedFile.FileName)

        ' upload file...
        Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
        clsStream.Write(bFile, 0, bFile.Length)
        clsStream.Close()
        clsStream.Dispose()
        '--------------------------


        lblUpload.Text = "Uploaded"
        btnSave.Focus()
    Else
        lblUpload.Text = "Choose a file to upload"
        btnSave.Focus()
    End If

99: 'Do Nothing

End Sub

The problem is you're trying to read in the PostedFile as a local file (on the web server), not from the HttpPostedFile object attached to the FileUploader.

Try:

Dim objFileStream As System.IO.Stream = FU.PostedFile.InputStream
Dim bFile(objFileStream.Length) As Byte
objFileStream.Read(bFile, 0, objFileStream.Length)

I tried something, and it worked..

            FU.SaveAs("C:\" & FU.FileName)

            '--------------------------
            ' set up request...

            Dim LocFile As String = FU.PostedFile.FileName
            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("MyFTP.com" & LocFile), System.Net.FtpWebRequest)

            clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

It worked.. simply saved the file from FU (FileUpload) to C:\ and then setting the address to always start at C:\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM