Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Lost on how you would upload a file in VB.net

Lost on how you would upload a file in VB.net

Scheduled Pinned Locked Moved Visual Basic
helptutorialcsharpasp-net
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DJ Matthews
    wrote on last edited by
    #1

    Hi all, I've been researching how to upload a file to a web server. I've found lots of articles about it (most here at CodeProject), but I'm having some problems with my code. Everytime I run the code I get the following error: The remote server returned an error: (404) Not Found. My Code:

        Dim responseArray As Byte()
        Try
            Dim WC As New System.Net.WebClient
            responseArray = WC.UploadFile("http://someserver/Uploads/upload.aspx", "POST",  "\\myfile.doc")
            txtResponce.Text = "Check the file at " + Encoding.ASCII.GetString(responseArray)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    

    What I don't understand is what "upload.aspx" is there for. There is never any code to go with it, so I'm not sure what it's there for.(MSDN has this same example, no code to upload.aspx). I've taken the "upload.aspx" out and I get the same error. I've given the ASPNET and ISS user accounts read/write access to the folder, so I'm sure it's not a permission problem. Can ANYONE explain what I'm doing wrong?

    L 1 Reply Last reply
    0
    • D DJ Matthews

      Hi all, I've been researching how to upload a file to a web server. I've found lots of articles about it (most here at CodeProject), but I'm having some problems with my code. Everytime I run the code I get the following error: The remote server returned an error: (404) Not Found. My Code:

          Dim responseArray As Byte()
          Try
              Dim WC As New System.Net.WebClient
              responseArray = WC.UploadFile("http://someserver/Uploads/upload.aspx", "POST",  "\\myfile.doc")
              txtResponce.Text = "Check the file at " + Encoding.ASCII.GetString(responseArray)
          Catch ex As Exception
              MsgBox(ex.ToString)
          End Try
      

      What I don't understand is what "upload.aspx" is there for. There is never any code to go with it, so I'm not sure what it's there for.(MSDN has this same example, no code to upload.aspx). I've taken the "upload.aspx" out and I get the same error. I've given the ASPNET and ISS user accounts read/write access to the folder, so I'm sure it's not a permission problem. Can ANYONE explain what I'm doing wrong?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      upload.aspx would be the webpage that would accept your file. The 404 error means you are trying to communicate to a server (or a document on a server) that does not exist.

      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

      D 1 Reply Last reply
      0
      • L Lost User

        upload.aspx would be the webpage that would accept your file. The 404 error means you are trying to communicate to a server (or a document on a server) that does not exist.

        Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

        D Offline
        D Offline
        DJ Matthews
        wrote on last edited by
        #3

        Ok, I finally figured it out. There is a property called Request.Files, It holds all of the data on the files that are being uploaded to that page. You can then loop through it and save the files to the web server. Code for anyone interested:

        Partial Class _Default
        Inherits System.Web.UI.Page

        Protected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Me.Request.Files.Count <> 0 then
                Dim I As Integer
                For I = 0 To Me.Request.Files.Count - 1
                    Me.Request.Files.Item(I).SaveAs("C:\\some\\path\\Uploads\\" & Me.Request.Files.Item(I).FileName)
                Next
            End If
        
        
        End Sub
        

        End Class

        I'm not sure that's the best way to go about it, but it works! I think I may write up an article about this as I couldn't find this information easily ANYWHERE.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups