FileUpload.HasFile プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileUpload.HasFile プロパティの意味・解説 

FileUpload.HasFile プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

ファイルが FileUpload コントロール格納されているかどうかを示す値を取得します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Dim instance As FileUpload
Dim value As Boolean

value = instance.HasFile
public bool HasFile { get;
 }
public:
property bool HasFile {
    bool get ();
}
/** @property */
public boolean get_HasFile ()

プロパティ
FileUploadファイル格納されている場合trueそれ以外場合false

解説解説
使用例使用例

エラー チェック実行する FileUpload コントロール作成する方法次のコード例示しますファイル保存する前にHasFile メソッド呼び出してアップロードするファイルコントロール格納されていることを確認します。さらに、File.Exists メソッド呼び出して、同じ名前のファイルが既にパス存在するかどうか確認します。同じ名前のファイル存在する場合は、アップロードするファイルの名前の前にアンダースコア (_) 文字追加してから、SaveAs メソッド呼び出します。これによって、既存ファイル上書きされないようにします。

<%@ Page Language="VB" %>

<html>
<head>

    <script runat="server">
        
      Sub UploadButton_Click(ByVal sender As
 Object, ByVal e As System.EventArgs)
            
        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.
        If (FileUpload1.HasFile) Then
          ' Call a helper method routine to save the file.
          SaveFile(FileUpload1.PostedFile)
        Else
          ' Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file
 to upload."
        End If

      End Sub
        
      Sub SaveFile(ByVal file As
 HttpPostedFile)
            
        ' Specify the path to save the uploaded file to.
        Dim savePath As String
 = "c:\temp\uploads\"
            
        ' Get the name of the file to upload.
        Dim fileName As String
 = FileUpload1.FileName
            
        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String
 = savePath + fileName
        
        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String
            
        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
          Dim counter As Integer
 = 2
          While (System.IO.File.Exists(pathToCheck))
            ' If a file with this name already exists,
            ' prefix the filename with a number.
            tempfileName = counter.ToString() + fileName
            pathToCheck = savePath + tempfileName
            counter = counter + 1
          End While
          
          fileName = tempfileName
          
          ' Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name
 already exists." + "<br>" + _
                                   "Your file was saved as "
 + fileName
          
        Else
          
          ' Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully."
          
        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName
            
        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)
            
      End Sub
        
  </script>

</head>
<body>

    <h3>FileUpload.SaveAs Method Example</h3>

    <form ID="Form1" runat="server">
   
        <h4>Select a file to upload:</h4>
       
        <asp:FileUpload id="FileUpload1"      
           
            runat="server">
        </asp:FileUpload>
            
        <br><br>
       
        <asp:Button id="UploadButton" 
            Text="Upload file"
            OnClick="UploadButton_Click"
            runat="server">
        </asp:Button>      
        
        <hr />
       
        <asp:Label id="UploadStatusLabel"
            runat="server">
        </asp:Label>   
         
    </form>

</body>
</html>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FileUpload クラス
FileUpload メンバ
System.Web.UI.WebControls 名前空間
SaveAs



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

FileUpload.HasFile プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileUpload.HasFile プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS