2012-04-25 93 views
1

我想上傳使用POST HTTP服務器上的文件,但是當我把它給了使用POST在HTTP服務器上傳文件給HttpFileCollection 0?

uploadFile.Count = 0 AMD不會進入if語句。文件路徑是正確的。

客戶端代碼

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      string fileToUpload = Server.MapPath("~/Files/Ricky_Martin_Livin_la.mp3"); 
      string uploadUrl = "http://localhost/soundcheck/uploadfiles.aspx"; 
      //string uploadUrl = "http://10.0.2.2/musicapp/handle_upload.php"; 
      FileStream rdr = new FileStream(fileToUpload, FileMode.Open); 
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl); 
      req.Method = "POST"; // you might use "POST" 
      req.ContentLength = rdr.Length; 
      req.AllowWriteStreamBuffering = true; 

      Stream reqStream = req.GetRequestStream(); 

      byte[] inData = new byte[rdr.Length]; 

      // Get data from upload file to inData 
      int bytesRead = rdr.Read(inData, 0, int.Parse(rdr.Length.ToString())); 

      // put data into request stream 
      reqStream.Write(inData, 0, int.Parse(rdr.Length.ToString())); 

      rdr.Close(); 
      req.GetResponse(); 

      // after uploading close stream 
      reqStream.Close(); 
     } 
    } 

Server代碼

using System; 
using System.Collections; 
using System.IO; 
using System.Data; 
using System.Web; 
using System.Text; 
using System.Web.Security; 

public partial class uploadfiles : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      LogInFile("First"); 
      HttpFileCollection uploadFile = Request.Files; 
      LogInFile("Second"); 
      LogInFile(uploadFile.Count.ToString()); 
      if (uploadFile.Count > 0) 
      { 
       HttpPostedFile postedFile = uploadFile[0]; 
       LogInFile("Thrid"); 
       System.IO.Stream inStream = postedFile.InputStream; 
       LogInFile("Forth"); 
       byte[] fileData = new byte[postedFile.ContentLength]; 
       LogInFile("Fifth"); 
       inStream.Read(fileData, 0, postedFile.ContentLength); 
       LogInFile("Sixth"); 
       postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName); 
      }   
     } 
     catch (Exception ex) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.AppendLine("Message : " +ex.Message); 
      sb.AppendLine("Source : " + ex.Source); 
      sb.AppendLine("StackTrace : " + ex.StackTrace); 
      sb.AppendLine("InnerException : " + ex.InnerException); 
      sb.AppendLine("ToString : " + ex.ToString()); 

      LogInFile(sb.ToString()); 
     } 
    } 
    public void LogInFile(string str) 
    { 
     StringBuilder sb = new StringBuilder(); 
     using (StreamReader sr = new StreamReader(Server.MapPath("Data") + "\\expfile.txt")) 
     {    
      sb.AppendLine("= = = = = ="); 
      sb.Append(sr.ReadToEnd()); 
      sb.AppendLine(); 
      sb.AppendLine(); 
     } 
     sb.AppendLine(str); 
     using (StreamWriter outfile = new StreamWriter(Server.MapPath("Data") + "\\expfile.txt")) 
     { 
      outfile.Write(sb.ToString()); 
     } 
    } 

} 

在服務器端的代碼我寫這些日誌來跟蹤如果在該行的任何錯誤或錯誤。

在服務器代碼,它給uploadFile.Count = 0和 不走在調試模式下的客戶端代碼執行成功,但給頁面

這個消息您可以從服務器/客戶端代碼中看到的..有沒有涉及數據庫。

enter image description here

+0

請張貼您的html表單。也登錄失敗的sa是asp.net無法登錄到你的db – 2012-04-25 05:13:17

+0

你可以從代碼中看到沒有涉及數據庫..或DB訪問代碼甚至 – Azhar 2012-04-25 06:32:31

+0

那麼你發佈的錯誤是數據庫相關。也許代碼在項目中的其他地方。也許global.asax? – 2012-04-25 07:27:40

回答

-1

我認爲您已提供的數據庫憑據不正確。

+0

你可以從代碼中看到不涉及數據庫..或數據庫訪問代碼甚至 – Azhar 2012-04-25 06:31:39

相關問題