2012-10-24 68 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