markus / UploadLicenseTest / MainWindow.xaml.cs @ master
이력 | 보기 | 이력해설 | 다운로드 (4.42 KB)
1 |
using Microsoft.Win32; |
---|---|
2 |
using Newtonsoft.Json; |
3 |
using System; |
4 |
using System.Collections.Specialized; |
5 |
using System.IO; |
6 |
using System.Net; |
7 |
using System.Net.Http; |
8 |
using System.Net.Http.Headers; |
9 |
using System.Text; |
10 |
using System.Threading.Tasks; |
11 |
using System.Windows; |
12 |
|
13 |
namespace UploadLicenseTest |
14 |
{ |
15 |
/// <summary> |
16 |
/// MainWindow.xaml에 대한 상호 작용 논리 |
17 |
/// </summary> |
18 |
public partial class MainWindow : Window |
19 |
{ |
20 |
public MainWindow() |
21 |
{ |
22 |
InitializeComponent(); |
23 |
} |
24 |
|
25 |
private void SelectFile_Click(object sender, RoutedEventArgs e) |
26 |
{ |
27 |
OpenFileDialog openFileDialog = new OpenFileDialog(); |
28 |
openFileDialog.Multiselect = false; |
29 |
|
30 |
var result = openFileDialog.ShowDialog(); |
31 |
|
32 |
if(result == true) |
33 |
{ |
34 |
txtFilename.Text = openFileDialog.FileName; |
35 |
} |
36 |
} |
37 |
|
38 |
private async void UploadFile_Click(object sender, RoutedEventArgs e) |
39 |
{ |
40 |
try |
41 |
{ |
42 |
string uri = "https://markuslicense.azurewebsites.net/Upload"; |
43 |
|
44 |
var uploadFile = await UploadFileAsync(uri, txtFilename.Text); |
45 |
var size = await FileSizeAsync(uri, uploadFile); |
46 |
|
47 |
txtError.AppendText(uploadFile + " " + size); |
48 |
txtError.AppendText("local Size = " + new FileInfo(txtFilename.Text).Length); |
49 |
txtError.LineDown(); |
50 |
} |
51 |
catch (Exception ex) |
52 |
{ |
53 |
txtError.AppendText(ex.ToString()); |
54 |
txtError.LineDown(); |
55 |
} |
56 |
} |
57 |
|
58 |
public async Task<string> UploadFileAsync(string _url,string filePath) |
59 |
{ |
60 |
try |
61 |
{ |
62 |
ServicePointManager.Expect100Continue = true; |
63 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
64 |
WebClient client = new WebClient(); |
65 |
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); |
66 |
client.Headers.Add("Cache-Control", "no-cache"); |
67 |
client.Encoding = Encoding.UTF8; |
68 |
|
69 |
var result = await client.UploadFileTaskAsync(new Uri("https://markuslicense.azurewebsites.net/Upload"), txtFilename.Text); |
70 |
string reply = System.Text.Encoding.UTF8.GetString(result); |
71 |
|
72 |
return reply; |
73 |
} |
74 |
catch (Exception) |
75 |
{ |
76 |
throw; |
77 |
} |
78 |
} |
79 |
|
80 |
|
81 |
public async Task<string> FileSizeAsync(string _url, string filePath) |
82 |
{ |
83 |
try |
84 |
{ |
85 |
ServicePointManager.Expect100Continue = true; |
86 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
87 |
WebClient client = new WebClient(); |
88 |
var data = await client.DownloadStringTaskAsync($"{_url}/FileInfo?filename={filePath}"); |
89 |
return data; |
90 |
} |
91 |
catch (Exception) |
92 |
{ |
93 |
throw; |
94 |
} |
95 |
} |
96 |
|
97 |
private void Client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e) |
98 |
{ |
99 |
string reply = System.Text.Encoding.UTF8.GetString(e.Result); |
100 |
|
101 |
txtError.LineDown(); |
102 |
txtError.AppendText(reply); |
103 |
txtError.AppendText("성공"); |
104 |
} |
105 |
|
106 |
private void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) |
107 |
{ |
108 |
txtError.LineDown(); |
109 |
txtError.AppendText($"Progress {e.ProgressPercentage}%"); |
110 |
|
111 |
} |
112 |
|
113 |
private async void GetFileInfo_Click(object sender, RoutedEventArgs e) |
114 |
{ |
115 |
try |
116 |
{ |
117 |
HttpClient client = new HttpClient(); |
118 |
var data = await client.GetStringAsync("https://markuslicense.azurewebsites.net/Upload/FileInfo?filename=Attachment 1. PDI-009_REV.B-Copy.pdf"); |
119 |
txtError.AppendText(data); |
120 |
} |
121 |
catch (Exception ex) |
122 |
{ |
123 |
txtError.AppendText(ex.ToString()); |
124 |
txtError.LineDown(); |
125 |
} |
126 |
} |
127 |
|
128 |
private void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) |
129 |
{ |
130 |
txtError.AppendText(e.Result.ToString()); |
131 |
} |
132 |
|
133 |
private void Client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) |
134 |
{ |
135 |
txtError.AppendText(e.Result.ToString()); |
136 |
} |
137 |
} |
138 |
} |