2

input type="file" 要素が 1 つあるフォームがあります。ファイルが選択されていない場合、HttpFileCollection のコレクションの背後にあるコードは空になると思います。

ただし、カウントは常にゼロより大きいようです。

次のコードで示されているように:

Dim files As HttpFileCollection = Request.Files

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

私は一般的な癖を経験していますか、それともこれは予期された動作ですか?

前もって感謝します。

4

1 に答える 1

-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)));
                }
         }
于 2012-07-17T12:01:18.737 に答える