Html 如何在 vb.net 中通过 HTTP post 发布文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/981354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post a file via HTTP post in vb.net
提问by Eduardo Molteni
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.
在 vb.net 中通过 HTTP post 发送文件时遇到问题。我试图模仿以下 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!
希望有人能帮忙!
回答by OneSHOT
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 应用程序将文件发布到 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
回答by Eduardo Molteni
You may use HttpWebRequestif UploadFile(as OneShot says) does not work out.
HttpWebRequest as more granular options for credentials, etc
如果UploadFile(如 OneShot 所说)不起作用,您可以使用HttpWebRequest。
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();
回答by pwDev
Use this to get your file from the HTTP Post.
使用它从 HTTP Post 获取您的文件。
Request.Files["File"];
回答by Bhaskar
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)
}