FileUpload.HasFile プロパティ
アセンブリ: System.Web (system.web.dll 内)


HasFile プロパティは、FileUpload コントロールにアップロードするファイルが格納されているかどうかを示す値を取得します。ファイルで操作を実行する前に、このプロパティを使用してアップロードするファイルが存在するかどうかを確認します。たとえば、SaveAs メソッドを呼び出してファイルをディスクに保存する前に、HasFile プロパティを使用してファイルが存在するかどうかを確認します。HasFile によって true が返される場合、SaveAs メソッドを呼び出します。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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からFileUpload.HasFile プロパティを検索する場合は、下記のリンクをクリックしてください。

- FileUpload.HasFile プロパティのページへのリンク