我需要在服务器上传文件(具有不同的扩展名,即.txt,.jpg,.pdf ....)。 我创建了一个接受http请求的站点,将虚拟目录映射到物理目录。 所有thi在dowload中工作正常,现在我必须实现上传。
这是我的代码
private void UploadFile(string uploadFileName, string localFileName)
{
//long length = 0;
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uploadFileName);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials = new NetworkCredential("USER", "PASSWORD", "DOMAIN");
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}";
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
string header = string.Format(headerTemplate, "uplTheFile", localFileName);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(localFileName, 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();
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
//error returned in lenght field: "This stream does not support seek operations."
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();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
//error returned from getResponse: "The remote server returned an error: (405) Method Not Allowed."
//I guess cause my folder is in read only
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show(reader2.ReadToEnd());
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
首先我收到此错误:远程服务器返回错误:(405)方法不允许。 所以我尝试通过在网站上添加映射来启用POST。
现在在te服务器上的文件夹中我有一个web.config文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<handlers>
<add name="File TXT" path="*.*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\System32\inetsrv\asp.dll" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
</handlers>
</system.webServer>
</configuration>
以这种方式我没有得到任何错误,但应用程序不上传任何文件。 我该如何解决这个问题?
答案 0 :(得分:0)
更简单的解决方案是使用System.Net.Webclient类上传文件。
System.Net.WebClient client = new System.Net.WebClient();
client.UploadFile("www.myfileuploadendpoint.com",@"c:\myfiletoupload.txt");
客户端还有一个OnUploadFileCompleted事件,该事件将在完成时调用处理程序。
client.OnUploadFileCompleted += UploadCompleted(sender,args) => { //do whatever you want}
这将为您节省代码和错误。祝好运! :)
答案 1 :(得分:-1)
上述两个样本均无效,
期待有人分享他们对这些例外的想法,请。
RGDS, 瑟尔