locked
Upload files on server from client machine (vb.net)

    Question

  • I want to create a windows application which can upload files from client machine to server. i.e. user can upload any file in his account from home using this application.

    thanx in advance

    vatsal

    Wednesday, October 25, 2006 9:22 AM

Answers

All replies

  • in .NET 2.0 there is an FTP Class/WebClient which may just do what you want it to do, to upload (and download) files.

    http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

    http://msdn2.microsoft.com/en-us/library/system.net.ftpwebresponse.aspx

    http://www.freevbcode.com/ShowCode.Asp?ID=4655

     

    I hope the above gets you started

    Wednesday, October 25, 2006 12:16 PM
    Moderator
  • Is there a way to do this using http posts?  I would like to impliment this same type of thing.  Except for some of the people who will be using my software they will be behind a firewall wich allows only out going and incomming http port 8080.  I imagine that you would use the upload command and then have some kind of a script on the other end.  Does anyone know of an example of this?  Are there asp.net commands on a web page that can handle this.  What I am wanting to do is this.  I have a webpage that they go to and I want it to be one click your done type of a thing.  I have the program download to their computer which runs creats a zip file and xml file.  I would also like to code to upload or pushes the file so that my clients don't have to browse to it as the directory is hidden and could cause some problems for those who are less computer savy.  So could I set the page up to be ready to recieve incomming posts as long as they are on that page?

    Also I am using c# not vb

    Wednesday, December 13, 2006 8:41 PM
  • I am looking for an example/sample code in VB.NET which shows how to upload IMAGE file from client's computer to web server. I have not find one that works in VB.NET.
    Sunday, July 01, 2007 5:54 AM
  • Is anybody now how to upload files on server using HTTPs?

     

    Tuesday, October 16, 2007 1:19 PM
  • I am not sure if this will help but this is how I upload file from client's computer without having them to browse the file and click upload button.

     

    I created activeX which includes below function.

    On my web server I have a file called upload.aspx (or it can be upload.jsp,upload.cfm, upload.php etc.) with a <file name= "fileUpload"> object and the code for either uploading to a drive or inserting into database. You can test your page by going to your upload page then do file browse and upload. If that works then you should be good.

     

    @@param strUploadfile = client's directory path eg. c:\image.jpg

    @@param url = https://www.blahblah.com/upload.aspx         

    @@objFileName = "fileUpload"

    @@contenttype = "multipart/form-data"

    @@ querystring = "userid=1&something=2&...."

     

    Public Function UploadFile(ByVal strUploadfile As String, ByVal url As String, ByVal objFileName As String, ByVal contenttype As String, ByVal querystring As String) As String
            If ((fileFormName = Nothing) Or (fileFormName.Length = 0)) Then
                fileFormName = "file"
            End If
            If ((contenttype = Nothing) Or (contenttype.Length = 0)) Then
                contenttype = "application/octet-stream"
            End If
            Dim postdata As String = "?" + querystring

            Dim uri As New Uri(url + postdata)
            'generate a unique boundary
            Dim boundary As String
            Dim strTemp As String
            strTemp = DateTime.Now.Ticks.ToString("x")
            boundary = StrDup(39 - strTemp.Length, "-") & strTemp
            'webRequest POST header
            Dim webrequest As HttpWebRequest = CType(HttpWebRequest.Create(uri), HttpWebRequest)
            webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"
            ' webrequest.Referer = "add some URL which i can check in cf page to see no body is directly calling it to upload virus"
            webrequest.Headers.Add("Accept-Language", "en-us")
            webrequest.ContentType = "multipart/form-data; boundary=" + boundary
            webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"
            webrequest.KeepAlive = True
            webrequest.Headers.Add("Cache-Control", "no-cache")
            webrequest.Headers.Add("Cookie", "popunder=yes")
            'webrequest.CookieContainer = cookies
            webrequest.Method = "POST"
            webrequest.ProtocolVersion = HttpVersion.Version10
            webrequest.AllowAutoRedirect = False
            'MIME Multipart Media Encapsulation
            Dim sb As New StringBuilder
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append(fileFormName)
            sb.Append("""; filename=""")
            sb.Append(Path.GetFileName(strUploadfile))
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append("Content-Type: ")
            sb.Append(contenttype)
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)
            Dim postHeader As String = sb.ToString()
            Dim postHeaderBytes As Byte() = Encoding.ASCII.GetBytes(postHeader)
            ' Build the trailing boundary string as a byte array
            ' ensuring the boundary appears on a line by itself
            Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & vbCrLf)
            'open file to upload
            Dim fileStream As New FileStream(strUploadfile, FileMode.Open, FileAccess.Read)
            'calculate length of POST
            Dim length As Long = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length
            webrequest.ContentLength = length
            'contact server with POST 1.0
            Dim requestStream As Stream = webrequest.GetRequestStream()
            ' Write out header to server
            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
            ''Application.DoEvents()
            ' Write out the file contents to server in chunks of 4K
            Dim buffer() As Byte
            ReDim buffer(Math.Min(4095, fileStream.Length))
            Dim bytesRead = 1 'initialize to any number except 0
            While bytesRead <> 0
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                If bytesRead <> 0 Then 'make sure EOF is not reached
                    requestStream.Write(buffer, 0, bytesRead)
                End If
                ''Application.DoEvents()
            End While
            ' Write out the trailing boundary
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
            requestStream.Close()
            'collect server response & convert to ASCII
            Dim responce As WebResponse = webrequest.GetResponse()
            Dim s As Stream = responce.GetResponseStream()
            Dim sr As New StreamReader(s)
            Return sr.ReadToEnd()
        End Function

    Tuesday, October 16, 2007 2:19 PM