How to post a file via HTTP post in vb.net

前端 未结 4 494
慢半拍i
慢半拍i 2020-12-05 03:47

Having a problem with sending a file via HTTP post in vb.net. I am trying to mimic the following HTML so the vb.net does the same thing.

相关标签:
4条回答
  • 2020-12-05 03:58

    Use this to get your file from the HTTP Post.

    Request.Files["File"];
    
    0 讨论(0)
  • 2020-12-05 04:17

    You could use the

    E.g:

    In ASPX:
    <Asp:FileUpload id="flUpload" runat="Server" />
    
    In Code Behind:
    if(flUpload.HasFile)
    {
      string filepath = flUpload.PostedFile.FileName;
      flUpload.PostedFile.SaveAs(Server.MapPath(".\\") + file)
    }
    
    0 讨论(0)
  • 2020-12-05 04:19

    You may use HttpWebRequest if UploadFile (as OneShot says) does not work out.
    HttpWebRequest as more granular options for credentials, etc

       FileStream rdr = new FileStream(fileToUpload, FileMode.Open);
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
       req.Method = "PUT"; // you might use "POST"
       req.ContentLength = rdr.Length;
       req.AllowWriteStreamBuffering = true;
    
       Stream reqStream = req.GetRequestStream();
    
       byte[] inData = new byte[rdr.Length];
    
       // Get data from upload file to inData 
       int bytesRead = rdr.Read(inData, 0, rdr.Length);
    
       // put data into request stream
       reqStream.Write(inData, 0, rdr.Length);
    
       rdr.Close();
       req.GetResponse();
    
       // after uploading close stream 
       reqStream.Close();
    
    0 讨论(0)
  • 2020-12-05 04:20

    I think what you are asking for is the ability to post a file to a web server cgi script from a VB.Net Winforms App.

    If this is so this should work for you

    Using wc As New System.Net.WebClient()
        wc.UploadFile("http://yourserver/cgi-bin/upload.cgi", "c:\test.bin")
    End Using
    
    0 讨论(0)
提交回复
热议问题