2012-07-17 94 views
2

我有一個表單,其中有1個input type =「file」元素。我希望當沒有選擇文件時,在HttpFileCollection集合後面的代碼將是空的。HttpFileCollection始終返回計數> 0

但是,似乎計數總是大於零。

爲證明與下面的代碼:

Dim files As HttpFileCollection = Request.Files 

     If files.Count > 0 Then 
      'At least one file has been uploaded 
     End if 

上午我遇到了一般怪癖,或者這是預期的行爲?

在此先感謝。

回答

-1

在.aspx頁面: -

 <form id="form1" runat="server" enctype="multipart/form-data"> 
    <input type="file" id="myFile" name="myFile" /> 
    <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" /> 
    </form> 

在你的代碼文件

 protected void btnUploadClick(object sender, EventArgs e) 
     { 
      HttpPostedFile file = Request.Files["myFile"]; 

      //check file was submitted 
      if (file != null && file.ContentLength > 0) 
       { 
        string fname = Path.GetFileName(file.FileName); 
        file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname))); 
       } 
     } 
+0

它是在C#的背後,我相信這將是罰款ü – RL89 2012-07-17 12:18:04

+0

據工作完美花花公子..你爲什麼投下我的答案? – RL89 2012-07-17 12:28:07