WinForm에서 WebClient이용 파일 업로드 하는 방법
IIS7, asp.net 기반으로 테스트 되었습니다.
우선 클라이언트 소스 입니다.
메인 소스(WinForm)파일입니다.
OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = "VideoFile(*.mp4)|*.mp4"; dlg.ShowDialog(); if (dlg.FileName != null) { try { WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; myWebClient.Encoding = Encoding.UTF8; byte[] fileContents = File.ReadAllBytes(dlg.FileName); Uri uri = new Uri("http://localhost/HttpUpload.aspx"); myWebClient.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback); myWebClient.UploadFileCompleted += new UploadFileCompletedEventHandler(ploadFileCompleted); // myWebClient.Credentials = new NetworkCredential("test", "test"); //사용자 인증을 위하여 //string cc = myWebClient.DownloadString(uri); //사용자 인증체크 위하여 인증 안되면 Exception 발생 // Console.WriteLine("--- : " + cc); myWebClient.UploadFileAsync(uri, "POST", dlg.FileName); Console.WriteLine("File upload started."); myWebClient.Dispose(); } catch (WebException ex) { Console.WriteLine("\nResponse Exception :\n{0}", ex.ToString()); } } |
파일 업로드 완료후 호출
void ploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { try { string Result = Encoding.UTF8.GetString(e.Result);//파일 업로드 완료후 리턴된 string Console.WriteLine("Return Value : " + Result); } catch (Exception ex) { Console.WriteLine("Return Execption : " + ex); }
} |
파일 업로드중 호출
void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e) { // Displays the operation identifier, and the transfer progress. //Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...", (string)e.UserState, e.BytesSent, e.TotalBytesToSend, Math.Truncate(((double)e.BytesSent / (double)e.TotalBytesToSend) * 100)); progressBar1.Value = int.Parse(Math.Truncate(((double)e.BytesSent / (double)e.TotalBytesToSend) * 100).ToString());
} |
요기 까지가 WinForm에서 WebClient를 이용하여 파일 업로드 모듈 입니다.
이젠 업로드 모듈을 만들었느니 서버쪽에서 받아 주는 파일을 만들어야 겠죠?
asp.net 소스 파일
string filename1 = ""; int FileCount1 = 1; string path = Server.MapPath("\\Upload\\"); foreach (string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files[f]; if (file.FileName.ToString().Trim() != "") { FileInfo f1 = new FileInfo(file.FileName); StringBuilder sb1 = new StringBuilder(path + f1.Name); FileInfo tempFile1; filename1 = f1.Name; do { tempFile1 = new FileInfo(sb1.ToString()); if (tempFile1.Exists) { filename1 = f1.Name.Replace(f1.Extension, FileCount1 + f1.Extension); StringBuilder sc1 = new StringBuilder(path + filename1); sb1 = sc1; FileCount1++; } } while (tempFile1.Exists); file.SaveAs(sb1.ToString()); } result.Text = "OK, fileName : " + filename1; }
|
보시면 아실수 있을꺼에요 쉬우니까요~ ㅎㅎ
그리고 참고로 web.config 에서 파일 업로드 셋팅을 해줘야 해요~
IIS 에서는 기본적으로 4M만 업로드 할수 있게 되어 있습니다. 그렇기 때문에
web.config 파일 셋팅 정보
<configuration> <system.web> <customErrors mode="Off"/> <compilation debug="false" targetFramework="4.0" /> <httpRuntime executionTimeout="110000" maxRequestLength="2147483646" /> </system.web> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483646"/> </requestFiltering> </security> </system.webServer> </configuration> |
이렇게~ 파일을 업로드 할수 있는 용량을 늘려 줘야 해요~
완성된 클라이언트 및 작용 장면입니다.
'Technique > Asp.net' 카테고리의 다른 글
보안을 위해 SHA-512 암호화 처리 (0) | 2013.07.19 |
---|