2

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
3
  • Sounds like an issue on the client side (i.e. your browser), not on the server side. Commented Apr 28, 2011 at 13:45
  • Could you post your markup of the FileUploader, the Open button, and event handlers for any events related to those controls? Commented Apr 28, 2011 at 13:48
  • msdn.microsoft.com/en-us/library/aa479405.aspx Commented Apr 28, 2011 at 13:49

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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:\

1 Comment

Yes, that will work, but you don't accomplish your goal of not having to use the server's disk. It would be nice if you would accept an answer on this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.