简体   繁体   English

如何通过vb.net中的HTTP发布来发布文件

[英]How to post a file via HTTP post in vb.net

Having a problem with sending a file via HTTP post in vb.net. 在vb.net中通过HTTP发布发送文件时遇到问题。 I am trying to mimic the following HTML so the vb.net does the same thing. 我正在尝试模仿以下HTML,以便vb.net做同样的事情。

<form enctype="multipart/form-data" method="post" action="/cgi-bin/upload.cgi">
File to Upload:
<input type="file" name="filename"/>
<input type="submit" value="Upload" name="Submit"/>
</form>

Hope someone can help! 希望有人能帮忙!

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. 我认为您所要求的是从VB.Net Winforms App将文件发布到Web服务器CGI脚本的能力。

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

You may use HttpWebRequest if UploadFile (as OneShot says) does not work out. 如果UploadFile (如OneShot所说)不起作用,则可以使用HttpWebRequest
HttpWebRequest as more granular options for credentials, etc HttpWebRequest作为凭据等更精细的选项

   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();

使用它可以从HTTP Post获取文件。

Request.Files["File"];

You could use the 您可以使用

Eg: 例如:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM