2012-07-17 93 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