2011-07-15 48 views
-4

我正在使用將文件自動傳輸到服務器的簡單應用程序進行文件上載控制。以編程方式將文件從客戶端上傳到服務器

我用作參考的那些示例Setting a file to upload inside the WebBrowser componentUploadFileEx但是當測試不會在服務器上創建文件!

表格代號:

// simulate this form 
     //<form action ="http://localhost/test.php" method = POST> 
     //<input type = text name = uname> 
     //<input type = password name =passwd> 
     //<input type = FILE name = uploadfile> 
     //<input type=submit> 

我發現後文件服務器將利用這個類的HttpWebRequest:

public static string UploadFileEx( string uploadfile, string url, 
     string fileFormName, string contenttype,NameValueCollection querystring, 
     CookieContainer cookies) 
    { 
     if((fileFormName== null) || 
      (fileFormName.Length ==0)) 
     { 
      fileFormName = "file"; 
     } 

     if((contenttype== null) || 
      (contenttype.Length ==0)) 
     { 
      contenttype = "application/octet-stream"; 
     } 


     string postdata; 
     postdata = "?"; 
     if (querystring!=null) 
     { 
      foreach(string key in querystring.Keys) 
      { 
       postdata+= key +"=" + querystring.Get(key)+"&"; 
      } 
     } 
     Uri uri = new Uri(url+postdata); 


     string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); 
     HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); 
     webrequest.CookieContainer = cookies; 
     webrequest.ContentType = "multipart/form-data; boundary=" + boundary; 
     webrequest.Method = "POST"; 


     // Build up the post message header 
     StringBuilder sb = new StringBuilder(); 
     sb.Append("--"); 
     sb.Append(boundary); 
     sb.Append("\r\n"); 
     sb.Append("Content-Disposition: form-data; name=\""); 
     sb.Append(fileFormName); 
     sb.Append("\"; filename=\""); 
     sb.Append(Path.GetFileName(uploadfile)); 
     sb.Append("\""); 
     sb.Append("\r\n"); 
     sb.Append("Content-Type: "); 
     sb.Append(contenttype); 
     sb.Append("\r\n"); 
     sb.Append("\r\n");   

     string postHeader = sb.ToString(); 
     byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); 

     // Build the trailing boundary string as a byte array 
     // ensuring the boundary appears on a line by itself 
     byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); 

     FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); 
     long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; 
     webrequest.ContentLength = length; 

     Stream requestStream = webrequest.GetRequestStream(); 

     // Write out our post header 
     requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); 

     // Write out the file contents 
     byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; 
     int bytesRead = 0; 
     while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) 
      requestStream.Write(buffer, 0, bytesRead); 

     // Write out the trailing boundary 
     requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); 
     requestStream.Flush(); 
     requestStream.Close(); 
     WebResponse responce = webrequest.GetResponse(); 
     Stream s = responce.GetResponseStream(); 
     StreamReader sr = new StreamReader(s); 

     return sr.ReadToEnd(); 

    } 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 
     CookieContainer cookies = new CookieContainer(); 
     //add or use cookies 

     NameValueCollection querystring = new NameValueCollection(); 

     querystring["uname"]=""; 
     querystring["passwd"]=""; 

     string uploadfile;// set to file to upload 

     uploadfile = "C:\\test.jpg"; 

     UploadFileEx(uploadfile, "http://127.0.0.1/app/Default.aspx", "uploadfile", "image/pjpeg", 
         querystring, cookies); 



    } 

在我的情況下,我想通過WebBrowser控件使用這個類,但是當我送崗位服務器文件(test.jpg)未創建!

這可能是文件夾權限問題!但是當使用IIS進行本地測試時,我遇到了同樣的問題?

感謝

+1

提供了一些代碼,太泛泛的問題。 – Tigran

+1

你可以發佈你使用的代碼嗎?你是否有任何具體的錯誤?您沒有給我們提供任何有用的信息,我們可以幫助您。 – Jason

+0

做這個鏈接中描述的內容:http://www.codeproject.com/KB/cs/uploadfileex.aspx UploadFileEx是一個自定義類。如果你需要幫助,你必須提供你的所有代碼。 –

回答

2

這是一個有點難以明白你的問題,很抱歉,如果這不是正確的答案,但如果你想使用標準張貼的WebRequest上傳一個文件,那麼它是這樣的:Upload files with HTTPWebrequest (multipart/form-data)

+0

我已經看過這段代碼,但是如何在請求中設置表單域。 – nayung

相關問題