locked
Upload File not working

    Domanda

  • I created a Windows Form in Visual Basic. I have the following code in my app. But its not working.

    Try
                Dim theUri As Uri = New Uri("http://myhostname.com/files/myfile.txt")
                My.Computer.Network.UploadFile("F:\TestFiles\myfile.txt", theUri, "myname", "mypassword", True, 500)
                MsgBox("Successfully Uploaded")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

    It gives me the following error message:

    "The remote server returned an error: (404) Not Found."

    I assume its because i dont have a file named "myfile.txt" in my server. So my question is this. How can i upload a file from my Harddisk?


    lunedì 5 marzo 2012 07:51

Risposte

  • Hi,

    It will not write directly to a server file. The server file is a server side upload page that will read the file content and will process this incoming content as needed.

    You have a server side page sample at http://msdn.microsoft.com/en-us/library/36s52zhs.aspx (WebClient.UploadFile, My.Computer.Network.UploadFile being likely just a wrapper around that). Then your UploadFile call will use this server side page as a POST target.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    lunedì 5 marzo 2012 13:15

Tutte le risposte

  • Hi,

    It will not write directly to a server file. The server file is a server side upload page that will read the file content and will process this incoming content as needed.

    You have a server side page sample at http://msdn.microsoft.com/en-us/library/36s52zhs.aspx (WebClient.UploadFile, My.Computer.Network.UploadFile being likely just a wrapper around that). Then your UploadFile call will use this server side page as a POST target.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    lunedì 5 marzo 2012 13:15
  •     Private Sub Upload_File()
            Try
                Dim webAdr As String = "http://myhostname.com/files/myfile.txt"
                Using wc As New WebClient
                    wc.Credentials = New NetworkCredential("Username""Password")
                    wc.UploadFile(webAdr, "F:\TestFiles\myfile.txt")
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~

    • Contrassegnato come risposta theronnie mercoledì 7 marzo 2012 07:29
    • Contrassegno come risposta annullato theronnie mercoledì 7 marzo 2012 07:29
    lunedì 5 marzo 2012 20:43
  •     Private Sub Upload_File()
            Try
                Dim webAdr As String = "http://myhostname.com/files/myfile.txt"
                Using wc As New WebClient
                    wc.Credentials = New NetworkCredential("Username""Password")
                    wc.UploadFile(webAdr, "F:\TestFiles\myfile.txt")
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~

    This looks promising. I will try it once i get my hands on VB in my home. Thank you very much both of you :)

    EDIT:

    Just tried it. This does not work. I added a MsgBox("Success") after End Using and it shows the MsgBox but the file is not uploaded. I tried two ways.

    I will explain what i need much more clearly :). I have a listview with a button that opens OpenFileDialog. When the user selects a file and press open, it should upload the file to my server. After uploading, the listview is refreshed to show all the files from a specific folder in my server

    Thats what i want to do. :)

    • Modificato theronnie giovedì 8 marzo 2012 14:17
    mercoledì 7 marzo 2012 07:30
  • Hi Theronnie,

    Thank you for your clarify.  Did you get the return value of upload method?  And did the file you uploaded not shown in the list? Only some advice can be given since we need more detailed information about your issue.

    I’d appreciate your feedback.

    Have a nice day!


    Shanks Zen
    MSDN Community Support | Feedback to us

    venerdì 9 marzo 2012 09:07
    Moderatore
  • Hi Theronnie,

    Thank you for your clarify.  Did you get the return value of upload method?  And did the file you uploaded not shown in the list? Only some advice can be given since we need more detailed information about your issue.

    I’d appreciate your feedback.

    Have a nice day!


    Shanks Zen
    MSDN Community Support | Feedback to us

    Hello Shanks Zen,

    I'll be glad to give the details. So this is the code i use:

    Private Sub Upload_File()
            Try
                Dim webAdr As String = "http://myhostname.com/files/myfile.txt"
                Using wc As New WebClient
                    wc.Credentials = New NetworkCredential("Username", "Password")
                    wc.UploadFile(webAdr, "F:\TestFiles\myfile.txt")
                End Using
                MsgBox("Uploaded")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    I get the "Uploaded" Message, but in my folder (On Server) nothing is there. I tried running the app in Admin mode but still not working.

    I want my clients to upload the file from their PC to my server. But somehow this method is not working. If you still dont understand what my issue is, please tell me. I will explain very briefly next time and even post screenshots! :)

    sabato 10 marzo 2012 18:17
  • Don't you have seen my previous answer ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    sabato 10 marzo 2012 21:47