FileUpload.FileName FileUpload.FileName FileUpload.FileName FileUpload.FileName Property

定義

FileUpload コントロールを使用して、アップロードする、クライアント上のファイルの名前を取得します。Gets the name of a file on a client to upload using the FileUpload control.

public:
 property System::String ^ FileName { System::String ^ get(); };
[System.ComponentModel.Browsable(false)]
public string FileName { get; }
member this.FileName : string
Public ReadOnly Property FileName As String

プロパティ値

FileUpload コントロールを使用して、アップロードする、クライアント上のファイルの名前を示す文字列。A string that specifies the name of a file on a client to upload using the FileUpload.

次の例では、作成する方法、FileUploadコードで指定されているパスにファイルを保存するコントロール。The following example demonstrates how to create a FileUpload control that saves files to a path that is specified in code. FileNameプロパティを使用して、アップロードするファイルの名前を取得します。The FileName property is used to get the name of the file to upload. SaveAsメソッドを呼び出して、サーバー上の指定したパスにファイルを保存する、ファイルがクライアント上に同じ名前を使用しています。The SaveAs method is called to save the file to the specified path on the server, using the same name the file has on the client. この例は、このコントロールの基本的な構文を示していますが、すべての必要なエラー チェックする場合は、ファイルを保存する前に行う必要がありますは示しませんに注意してください。Note that this example demonstrates the basic syntax for this control, but does not demonstrate all the necessary error checking that should be done before saving the file. この例より完全なコード例については、「SaveAs」を参照してください。For a more complete example, see SaveAs.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server">

  protected void UploadButton_Click(object sender, EventArgs e)
  {
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";
 
    // Before attempting to perform operations
    // on the file, verify that the FileUpload 
    // control contains a file.
    if (FileUpload1.HasFile)
    {
      // Get the name of the file to upload.
      String fileName = FileUpload1.FileName;
      
      // 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 path.
      // This example does not perform all
      // the necessary error checking.               
      // If a file with the same name
      // already exists in the specified path,  
      // the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath);
      
      // Notify the user of the name of the file
      // was saved under.
      UploadStatusLabel.Text = "Your file was saved as " + fileName;
    }
    else
    {      
      // Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <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>        
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server">
        
  Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            
    ' Specify the path on the server to
    ' save the uploaded file to.
    Dim savePath As String = "c:\temp\uploads\"
            
    ' Before attempting to perform operations
    ' on the file, verify that the FileUpload 
    ' control contains a file.
    If (FileUpload1.HasFile) Then
      ' Get the name of the file to upload.
      Dim fileName As String = FileUpload1.FileName
                      
      ' 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 path.
      ' This example does not perform all
      ' the necessary error checking.               
      ' If a file with the same name
      ' already exists in the specified path,  
      ' the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath)
                
      ' Notify the user of the name the file
      ' was saved under.
      UploadStatusLabel.Text = "Your file was saved as " & fileName
                
    Else
      ' Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload."
    End If

  End Sub
       
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <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>        
    </div>
    </form>
</body>
</html>

注釈

FileUpload コントロールを使用して、アップロードする、クライアント上のファイルの名前を取得します。Gets the name of a file on a client to upload using the FileUpload control. ファイル名をFileNameプロパティは、クライアント上のファイルのパスを含まないを返します。The file name that the FileName property returns does not include the path of the file on the client.

適用対象

こちらもご覧ください