본문 바로가기
[ Program ]/C#

Windows Forms에서 웹서버로 파일 업로드 하기

by 관이119 2015. 2. 25.

 

 

출처 - http://walnuttree.tistory.com/99

 

 

윈폼에서 웹서버로 파일을 업로드 하는 방법이다
(물론 FTP로 바로 올리는 방법도 있다)
지금 설명하는 방법으로 파일을 올리기 위해선 두가지 작업(윈폼, 웹)을 해줘야 한다.

먼저, 윈폼에선 다음과 같이 코드를 작성한다.

OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "Microsoft Excel|*.jpg";
dlg.ShowDialog();

System.Net.WebClient wcClient = new System.Net.WebClient();

wcClient.UploadFile(웹상에서 파일 업로드를 구현한 페이지 주소, "POST", dlg.FileName);     
(예를 들면 http://localhost:58438/WebSite4/FileUpload.aspx 과 같다)


다음은 웹단을 코드를 작성해 보자
(예:http://localhost:58438/WebSite4/FileUpload.aspx)

protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string f in Request.Files.AllKeys)
        {
            HttpPostedFile file = Request.Files[f];
            file.SaveAs(업로드 파일을 저장할 경로 + file.FileName);
(예를 들면 "D:\\wwwroot\\WebSite4\\Data\\"과 같다)
        }   
    }


이제 윈폼을 실행해 보면 웹서버로 파일이 업로드 되는것을 확인할수 있다.

 

 

----------------------------------------------------------------------------------------------------------------------------

 

 

위와같이해서 웹서버에 올리면 아래와 같은 에러가 나는 경우가 있다.

Server Error in '/' Application.


Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

 

 

 

이경우는 서버의  web.config 파일에 다음과 같이 추가해주면 된다.

<configuration>

    <system.web>

        <customErrors mode="Off"/>

    </system.web>

</configuration>

 

 

 

댓글