2012-10-24 67 views
0

我需要在服务器上上传文件(使用不同的扩展名,例如.txt,.jpg,.pdf ....)。 我创建了一个接受http请求并将虚拟目录映射到物理目录的站点。 所有在下载中工作正常,现在我必须执行上传。在httpwebrequest文件上传中启用POST

这里是我的代码

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

你不能保存文件吗?你使用的是Asp.NET文件上传控件吗?你需要将文件发送到另一个端点吗? – awright18

+0

你是什么意思“只保存文件”?我需要从客户端上传到服务器文件夹。不,我没有使用Asp.NET,只是应用程序池中由httpwebrequest从客户端调用的网站(不知道如果我完全回答你的问题) – andrea

+0

对不起安德烈我不能读... – awright18

回答

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} 

这将为您节省代码和错误。祝你好运! :)

+0

得到同样的行为。 1)POST不允许,添加web.confing后没有错误,但没有上传文件 – andrea

+0

后端使用WCF偶然? – awright18

+0

认为这可能是相关的http://stackoverflow.com/questions/2436182/405-method-not-allowed-error-in-wcf – awright18

-1

两个不工作的上述样品,

  1. “远程服务器返回错误:(405)不允许的方法”。是尝试使用上述代码时遇到的异常

期待有人分享他们对这些例外的想法,请。

rgds, thiru