WebClient.UploadFile 메서드

정의

지정된 URI를 사용하여 리소스에 로컬 파일을 업로드합니다.

오버로드

UploadFile(String, String)

지정된 URI를 사용하여 리소스에 지정된 로컬 파일을 업로드합니다.

UploadFile(Uri, String)

지정된 URI를 사용하여 리소스에 지정된 로컬 파일을 업로드합니다.

UploadFile(String, String, String)

지정된 메서드를 사용하여 지정된 리소스에 지정된 로컬 파일을 업로드합니다.

UploadFile(Uri, String, String)

지정된 메서드를 사용하여 지정된 리소스에 지정된 로컬 파일을 업로드합니다.

UploadFile(String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

지정된 URI를 사용하여 리소스에 지정된 로컬 파일을 업로드합니다.

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ fileName);
public byte[] UploadFile (string address, string fileName);
member this.UploadFile : string * string -> byte[]
Public Function UploadFile (address As String, fileName As String) As Byte()

매개 변수

address
String

파일을 받을 리소스의 URI입니다. 예를 들어, ftp://localhost/samplefile.txt입니다.

fileName
String

리소스에 보낼 파일입니다. 예를 들면 "samplefile.txt"입니다.

반환

Byte[]

리소스의 응답 본문이 포함된 Byte 배열입니다.

예외

address 매개 변수가 null인 경우

또는

fileName 매개 변수가 null인 경우

BaseAddressaddress를 조합하여 만든 URI가 잘못된 경우

또는

fileNamenull 또는 Empty이거나, 잘못된 문자를 포함하거나, 없는 경우

또는

파일을 업로드하는 동안 오류가 발생한 경우

또는

리소스를 호스팅하는 서버에서 응답이 없는 경우

또는

Content-type 헤더가 multipart로 시작되는 경우

예제

다음 코드 예제에서는 를 사용하여 UploadFile지정된 파일을 지정된 URI에 업로드합니다. 서버에서 반환된 모든 응답이 콘솔에 표시됩니다.

Console::Write( "\nPlease enter the URI to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the fully qualified path of the file to be uploaded to the URI" );
String^ fileName = Console::ReadLine();
Console::WriteLine( "Uploading {0} to {1} ...", fileName, uriString );

// Upload the file to the URI.
// The 'UploadFile(uriString, fileName)' method implicitly uses HTTP POST method.
array<Byte>^responseArray = myWebClient->UploadFile( uriString, fileName );

// Decode and display the response.
Console::WriteLine( "\nResponse Received::The contents of the file uploaded are: \n {0}", 
    System::Text::Encoding::ASCII->GetString( responseArray ) );
Console.Write("\nPlease enter the URI to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);

// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}", 
    System.Text.Encoding.ASCII.GetString(responseArray));

Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

Console.WriteLine(ControlChars.Cr & _
    "Please enter the fully qualified path of the file to be uploaded to the URI")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the URI.
' The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method. 
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr & "Response Received. The contents of the file uploaded are: " & _
    ControlChars.Cr & "{0}", System.Text.Encoding.ASCII.GetString(responseArray))

다음 코드 예제에서는 게시 된 파일을 허용 하 고 사용 하는 데 적합 한 ASP.NET 페이지를 보여 집니다 UploadFile 는 메서드. 페이지는 웹 서버에 있어야 합니다. 해당 주소는 메서드의 UploadFile 매개 변수에 address 대한 값을 제공합니다.

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
    
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }	
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f
        
    End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>

설명

메서드는 UploadFile 리소스에 로컬 파일을 보냅니다. 이 메서드는 STOR 명령을 사용하여 FTP 리소스를 업로드합니다. HTTP 리소스의 경우 POST 메서드가 사용됩니다.

이 메서드는 파일을 업로드하는 동안 차단합니다. 서버의 응답을 기다리는 동안 실행을 계속하려면 메서드 중 UploadFileAsync 하나를 사용합니다.

메서드는 POST HTTP로 정의됩니다. 기본 요청이 HTTP POST 를 사용하지 않고 서버에서 이해하지 못하는 경우 기본 프로토콜 클래스가 발생하는 작업을 결정합니다. 일반적으로 WebException 는 오류를 나타내기 위해 속성이 설정된 상태에서 Status throw됩니다.

속성이 BaseAddress 빈 문자열("")이 아니고 address 절대 URI를 포함하지 않는 경우 는 요청된 데이터의 절대 URI address 를 형성하기 위해 와 BaseAddress 결합된 상대 URI여야 합니다. 속성이 QueryString 빈 문자열이 아니면 에 추가됩니다 address.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상

UploadFile(Uri, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

지정된 URI를 사용하여 리소스에 지정된 로컬 파일을 업로드합니다.

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ fileName);
public byte[] UploadFile (Uri address, string fileName);
member this.UploadFile : Uri * string -> byte[]
Public Function UploadFile (address As Uri, fileName As String) As Byte()

매개 변수

address
Uri

파일을 받을 리소스의 URI입니다. 예를 들어, ftp://localhost/samplefile.txt입니다.

fileName
String

리소스에 보낼 파일입니다. 예를 들면 "samplefile.txt"입니다.

반환

Byte[]

리소스의 응답 본문이 포함된 Byte 배열입니다.

예외

address 매개 변수가 null인 경우

또는

fileName 매개 변수가 null인 경우

BaseAddressaddress를 조합하여 만든 URI가 잘못된 경우

또는

fileNamenull 또는 Empty이거나, 잘못된 문자를 포함하거나, 없는 경우

또는

파일을 업로드하는 동안 오류가 발생한 경우

또는

리소스를 호스팅하는 서버에서 응답이 없는 경우

또는

Content-type 헤더가 multipart로 시작되는 경우

설명

메서드는 UploadFile 리소스에 로컬 파일을 보냅니다. 이 메서드는 STOR 명령을 사용하여 FTP 리소스를 업로드합니다. HTTP 리소스의 경우 POST 메서드가 사용됩니다.

이 메서드는 파일을 업로드하는 동안 차단합니다. 서버의 응답을 기다리는 동안 실행을 계속하려면 메서드 중 UploadFileAsync 하나를 사용합니다.

메서드는 POST HTTP로 정의됩니다. 기본 요청이 HTTP POST 를 사용하지 않고 서버에서 이해하지 못하는 경우 기본 프로토콜 클래스가 발생하는 작업을 결정합니다. 일반적으로 WebException 는 오류를 나타내기 위해 속성이 설정된 상태에서 Status throw됩니다.

속성이 BaseAddress 빈 문자열("")이 아니고 address 절대 URI를 포함하지 않는 경우 는 요청된 데이터의 절대 URI address 를 형성하기 위해 와 BaseAddress 결합된 상대 URI여야 합니다. 속성이 QueryString 빈 문자열이 아니면 에 추가됩니다 address.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상

UploadFile(String, String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

지정된 메서드를 사용하여 지정된 리소스에 지정된 로컬 파일을 업로드합니다.

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (string address, string? method, string fileName);
public byte[] UploadFile (string address, string method, string fileName);
member this.UploadFile : string * string * string -> byte[]
Public Function UploadFile (address As String, method As String, fileName As String) As Byte()

매개 변수

address
String

파일을 받을 리소스의 URI입니다.

method
String

리소스에 파일을 보내는 데 사용되는 메서드입니다. null인 경우 http의 기본값은 POST이고 ftp의 기본값은 STOR입니다.

fileName
String

리소스에 보낼 파일입니다.

반환

Byte[]

리소스의 응답 본문이 포함된 Byte 배열입니다.

예외

address 매개 변수가 null인 경우

또는

fileName 매개 변수가 null인 경우

BaseAddressaddress를 조합하여 만든 URI가 잘못된 경우

또는

fileNamenull 또는 Empty이거나, 잘못된 문자를 포함하거나, 없는 경우

또는

파일을 업로드하는 동안 오류가 발생한 경우

또는

리소스를 호스팅하는 서버에서 응답이 없는 경우

또는

Content-type 헤더가 multipart로 시작되는 경우

예제

다음 코드 예제에서는 를 사용하여 UploadFile지정된 파일을 지정된 URI에 업로드합니다. 서버에서 반환된 모든 응답이 콘솔에 표시됩니다.

Console::Write( "\nPlease enter the URL to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine
    ("\nPlease enter the fully qualified path of the file to be uploaded to the URL" );
String^ fileName = Console::ReadLine();
Console::WriteLine( "Uploading {0} to {1} ...", fileName, uriString );

// Upload the file to the URL using the HTTP 1.0 POST.
array<Byte>^responseArray = myWebClient->UploadFile( uriString, "POST", fileName );

// Decode and display the response.
Console::WriteLine( "\nResponse Received::The contents of the file uploaded are: \n {0}", 
    System::Text::Encoding::ASCII->GetString( responseArray ));
Console.Write("\nPlease enter the URL to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);						
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}",
    System.Text.Encoding.ASCII.GetString(responseArray));


Console.Write(ControlChars.Cr + "Please enter the URL to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr & _
    "Please enter the fully qualified path of the file to be uploaded to the URL")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the Url using the HTTP 1.0 POST.
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, "POST", fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response Received. The contents of the file uploaded are: " & _
    ControlChars.Cr & "{0}", System.Text.Encoding.ASCII.GetString(responseArray))

다음 코드 예제에서는 게시 된 파일을 허용 하 고 사용 하는 데 적합 한 ASP.NET 페이지를 보여 집니다 UploadFile 는 메서드. 페이지는 웹 서버에 있어야 합니다. 해당 주소는 메서드의 UploadFile 매개 변수에 address 대한 값을 제공합니다.

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
    
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }	
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f
        
    End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>

설명

주소가 HTTP 리소스를 지정하는 경우 메서드는 UploadFile 매개 변수에 지정된 HTTP 메서드를 사용하여 리소스에 method 로컬 파일을 보내고 서버의 응답을 반환합니다. 이 메서드는 파일을 업로드하는 동안 차단합니다. 서버의 응답을 기다리는 동안 실행을 계속하려면 메서드 중 UploadFileAsync 하나를 사용합니다.

매개 변수가 method 서버 또는 address 리소스에서 인식할 수 없는 동사를 지정하는 경우 기본 프로토콜 클래스는 어떤 일이 발생하는지 결정합니다. 일반적으로 WebException 는 오류를 나타내기 위해 속성이 설정된 상태에서 Status throw됩니다.

속성이 BaseAddress 빈 문자열("")이 아니고 address 절대 URI를 포함하지 않는 경우 는 요청된 데이터의 절대 URI address 를 형성하기 위해 와 BaseAddress 결합된 상대 URI여야 합니다. 속성이 QueryString 빈 문자열이 아니면 에 추가됩니다 address.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상

UploadFile(Uri, String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

지정된 메서드를 사용하여 지정된 리소스에 지정된 로컬 파일을 업로드합니다.

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (Uri address, string? method, string fileName);
public byte[] UploadFile (Uri address, string method, string fileName);
member this.UploadFile : Uri * string * string -> byte[]
Public Function UploadFile (address As Uri, method As String, fileName As String) As Byte()

매개 변수

address
Uri

파일을 받을 리소스의 URI입니다.

method
String

리소스에 파일을 보내는 데 사용되는 메서드입니다. null인 경우 http의 기본값은 POST이고 ftp의 기본값은 STOR입니다.

fileName
String

리소스에 보낼 파일입니다.

반환

Byte[]

리소스의 응답 본문이 포함된 Byte 배열입니다.

예외

address 매개 변수가 null인 경우

또는

fileName 매개 변수가 null인 경우

BaseAddressaddress를 조합하여 만든 URI가 잘못된 경우

또는

fileNamenull 또는 Empty이거나, 잘못된 문자를 포함하거나, 없는 경우

또는

파일을 업로드하는 동안 오류가 발생한 경우

또는

리소스를 호스팅하는 서버에서 응답이 없는 경우

또는

Content-type 헤더가 multipart로 시작되는 경우

설명

주소가 HTTP 리소스를 지정하는 경우 메서드는 UploadFile 매개 변수에 지정된 HTTP 메서드를 사용하여 리소스에 method 로컬 파일을 보내고 서버의 응답을 반환합니다. 이 메서드는 파일을 업로드하는 동안 차단합니다. 서버의 응답을 기다리는 동안 실행을 계속하려면 메서드 중 UploadFileAsync 하나를 사용합니다.

매개 변수가 method 서버 또는 address 리소스에서 인식할 수 없는 동사를 지정하는 경우 기본 프로토콜 클래스는 어떤 일이 발생하는지 결정합니다. 일반적으로 WebException 는 오류를 나타내기 위해 속성이 설정된 상태에서 Status throw됩니다.

속성이 BaseAddress 빈 문자열("")이 아니고 address 절대 URI를 포함하지 않는 경우 는 요청된 데이터의 절대 URI address 를 형성하기 위해 와 BaseAddress 결합된 상대 URI여야 합니다. 속성이 QueryString 빈 문자열이 아니면 에 추가됩니다 address.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상