我有一个小型的C#web应用程序。如何获取允许用户通过HTTP POST发送文件的c#代码。它应该能够发送文本文件,图像文件,excel,csv ,doc(所有类型的文件),而不使用流读取器和所有。在c#中使用HTTP POST发送文件
回答
你可以试试下面的代码:
public void PostMultipleFiles(string url, string[] files)
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes =System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary +"\r\n");
string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
memStream.Write(boundarybytes, 0, boundarybytes.Length);
for (int i = 0; i < files.Length; i++)
{
string header = string.Format(headerTemplate, "file" + i, files[i]);
//string header = string.Format(headerTemplate, "uplTheFile", files[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
try
{
WebResponse webResponse = httpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string var = reader.ReadToEnd();
}
catch (Exception ex)
{
response.InnerHtml = ex.Message;
}
httpWebRequest = null;
}
只是倾销50行代码不是答案。这个问题有很多答案,许多这样的代码已经内置在.NET类型中,比如WebClient和HttpClient。 – CodeCaster
你能解释一下接收者页面的检索过程吗? –
你可以举一个URL的例子.. 它包含文件名吗? –
试试这个
string fileToUpload = @"c:\user\test.txt";
string url = "http://example.com/upload";
using (var client = new WebClient())
{
byte[] result = client.UploadFile(url, fileToUpload);
string responseAsString = Encoding.Default.GetString(result);
}
使用.NET 4.5(或通过添加的NuGet的Microsoft.Net.Http包.NET 4.0)有一个更简单的方法模拟表单请求。这里有一个例子:
private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
HttpContent stringContent = new StringContent(paramString);
HttpContent fileStreamContent = new StreamContent(paramFileStream);
HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(stringContent, "param1", "param1");
formData.Add(fileStreamContent, "file1", "file1");
formData.Add(bytesContent, "file2", "file2");
var response = client.PostAsync(actionUrl, formData).Result;
if (!response.IsSuccessStatusCode)
{
return null;
}
return response.Content.ReadAsStreamAsync().Result;
}
}
是否必须始终使用多部分表单数据? – Abhijeet
- 1. 使用HTTP POST发送文件> 1MB
- 2. 使用C++发送使用http POST的文件
- 3. 发送和使用HTTP POST
- 4. 使用System.Net.WebClient发送HTTP POST
- 5. 如何在android中使用http post方法发送文件?
- 6. 无法在angularJS中使用$ http post方法发送文件
- 7. 在Windows中使用C发送HTTP GET或POST请求
- 8. EBAY - 通过http post发送文件?
- 9. 在C#中使用PHP POST发送文件#
- 10. 在C++中使用POST [无法上载]发送文件
- 11. 用C/C++中的winsocks发送原始请求(http post文件数据)
- 12. 使用http-conduit在Haskell中发送HTTP/POST的基本方法
- 13. 发送json http post
- 14. 如何使用http post发送文件?德尔福2009年
- 15. 使用http-post发送xml文件到处理程序
- 16. Http POST如何使用multipart/form-data发送JSON文件内部
- 17. 使用HTTP POST将文件发送到IIS 7.5虚拟目录
- 18. 如何使用HTTParty(Net :: HTTP)向服务器发送文件发送POST请求
- 19. 通过HTTP POST发送一个文件与C#
- 20. 使用HttpURLConnection从Android发送HTTP POST
- 21. 使用Http Post发送图像
- 22. 使用HTTP发送ASCII字符串Post
- 23. 如何使用curl发送HTTP POST?
- 24. 使用NSURLSession HTTP发送Firebase Swift Push Post
- 25. 发送HTTP POST请求使用PHP
- 26. 使用HTTP发送ASCII字符串Post
- 27. HTTP 415使用POST发送JSON对象
- 28. 使得XML在HTTP POST golang发送
- 29. 在WCF中发送HTTP POST中的Xml
- 30. 使用C++ REST SDK发送更新文件内容的HTTP POST请求Casablanca
http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data –
'我已经试过各种方法,但没有人帮助我。 ' - 如果你展示了一些你已经尝试过的方法,我们可能会发现他们有什么问题。现在,很难进行建设性的讨论。 –
重复[使用C#通过HTTP POST发送文件](http://stackoverflow.com/questions/1131425/send-a-file-via-http-post-with-c-sharp)和其他各种。显示你曾经尝试过的,以及upvoters,请考虑你是upvoting的问题是一个受欢迎的除了网站或只是一个副本。 – CodeCaster