Share via


FileUpload 웹 서버 컨트롤 선언

업데이트: 2007년 11월

서버에 업로드할 파일을 사용자가 선택할 수 있는 <input type=file> 컨트롤을 만듭니다. 일반적으로 이 컨트롤은 텍스트 상자 컨트롤과 찾아보기 단추로 표시됩니다.

<asp:FileUpload
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CssClass="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    Visible="True|False"
    Width="size"
/>

설명

FileUpload 컨트롤은 사용자가 클라이언트에서 파일을 선택하여 웹 서버에 업로드할 수 있도록 하는 텍스트 상자 컨트롤과 찾아보기 단추를 표시합니다. 컨트롤의 텍스트 상자에 로컬 컴퓨터에 있는 파일의 전체 경로(예: C:\MyFiles\TestFile.txt)를 입력하여 업로드 대상 파일을 지정할 수 있습니다. 또는 찾아보기 단추를 클릭한 다음 파일 선택 대화 상자에서 해당 위치를 찾는 방법으로 파일을 선택할 수 있습니다.

FileUpload 컨트롤은 사용자가 업로드하기 위해 선택한 파일을 서버에 자동으로 보내지 않습니다. 그러므로 폼을 전송할 수 있는 컨트롤이나 메커니즘을 명시적으로 제공해야 합니다. 일반적으로 파일은 저장되거나, 서버에 대한 다시 게시를 발생시키는 이벤트에 대한 이벤트 처리 메서드에서 내용이 처리됩니다. 예를 들어, 파일 전송 단추를 제공할 경우에는 파일을 저장하는 코드를 클릭 이벤트에 대한 이벤트 처리 메서드 안에 넣을 수 있습니다. FileUpload 컨트롤에 대한 자세한 내용은 방법: FileUpload 웹 서버 컨트롤을 사용하여 파일 업로드를 참조하십시오.

예제

다음 코드 예제에서는 코드에 지정된 경로에 파일을 저장하는 FileUpload 컨트롤을 만드는 방법을 보여 줍니다. 여기에서는 서버의 지정된 경로에 파일을 저장하기 위해 SaveAs 메서드가 호출됩니다. 이 예제가 포함된 ASP.NET 응용 프로그램에는 서버의 지정된 디렉터리에 대한 쓰기 권한이 있어야 합니다. 업로드된 파일이 저장될 디렉터리에서 응용 프로그램을 실행하는 계정에 쓰기 권한을 명시적으로 부여할 수 있습니다. 또는 ASP.NET 응용 프로그램에 부여된 신뢰 수준을 높게 설정할 수 있습니다.

<%@ 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>
<%@ 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>

참고 항목

개념

FileUpload 웹 서버 컨트롤 개요

참조

FileUpload