markus / ConvertService / ServiceBase / Markus.Service.Extensions / Helper / GetDownloadFileName.cs @ 77cdac33
이력 | 보기 | 이력해설 | 다운로드 (2.92 KB)
1 | 06f13e11 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Linq; |
||
5 | using System.Text; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Web; |
||
8 | |||
9 | namespace Markus.Service.Helper |
||
10 | { |
||
11 | public static class DownloadUri |
||
12 | { |
||
13 | public static string GetFileName(string uri) |
||
14 | { |
||
15 | string FileName = ""; |
||
16 | |||
17 | if (!uri.ToLower().Contains("filename") && uri.Contains("VPCS_DOCLIB")) |
||
18 | { |
||
19 | if (!Path.IsPathRooted(uri)) |
||
20 | { |
||
21 | FileName = uri.Remove(0, uri.LastIndexOf("/") + 1); |
||
22 | } |
||
23 | else |
||
24 | { |
||
25 | FileName = uri.Remove(0, uri.LastIndexOf("\\") + 1); |
||
26 | } |
||
27 | } |
||
28 | else if (uri.ToLower().Contains("filename") && !uri.Contains("VPCS_DOCLIB")) |
||
29 | { |
||
30 | FileName = HttpUtility.ParseQueryString(new Uri(uri).Query).Get("filename"); |
||
31 | } |
||
32 | else |
||
33 | { |
||
34 | FileName = GetDownloadFileName(uri); |
||
35 | } |
||
36 | |||
37 | FileName = HttpUtility.UrlDecode(FileName); |
||
38 | |||
39 | return FileName; |
||
40 | } |
||
41 | |||
42 | 0a89a17f | taeseongkim | public static string GetDownloadFileName(string downloadUri) |
43 | 06f13e11 | taeseongkim | { |
44 | string fileName = ""; |
||
45 | 0a89a17f | taeseongkim | |
46 | 06f13e11 | taeseongkim | System.Net.WebClient wc = new System.Net.WebClient(); |
47 | var data = wc.DownloadData(downloadUri); |
||
48 | |||
49 | if (!String.IsNullOrEmpty(wc.ResponseHeaders["Content-Disposition"])) |
||
50 | { |
||
51 | var contentDisposition = wc.ResponseHeaders["Content-Disposition"]; |
||
52 | fileName = GetFileNameInDisposition(new System.Net.Mime.ContentDisposition(contentDisposition)); |
||
53 | } |
||
54 | 0a89a17f | taeseongkim | else |
55 | { |
||
56 | var contenttype = wc.ResponseHeaders[System.Net.HttpResponseHeader.ContentType]; |
||
57 | |||
58 | if (contenttype == "application/pdf") |
||
59 | { |
||
60 | fileName = downloadUri.Remove(0, downloadUri.LastIndexOf("/") + 1); |
||
61 | } |
||
62 | } |
||
63 | 06f13e11 | taeseongkim | |
64 | return fileName; |
||
65 | } |
||
66 | |||
67 | 77cdac33 | taeseongkim | public static string GetFileNameInDisposition(System.Net.Mime.ContentDisposition contentDisposition) |
68 | 06f13e11 | taeseongkim | { |
69 | if (contentDisposition.FileName != null) |
||
70 | { |
||
71 | return contentDisposition.FileName; |
||
72 | } |
||
73 | |||
74 | var fileName = contentDisposition.Parameters["filename*"]; |
||
75 | if (fileName == null) |
||
76 | { |
||
77 | return null; |
||
78 | } |
||
79 | |||
80 | var pos = fileName.IndexOf("''", StringComparison.InvariantCulture); |
||
81 | var encoding = Encoding.UTF8; |
||
82 | if (pos >= 0) |
||
83 | { |
||
84 | try |
||
85 | { |
||
86 | encoding = Encoding.GetEncoding(fileName.Substring(0, pos)); |
||
87 | } |
||
88 | catch (ArgumentException) |
||
89 | { |
||
90 | } |
||
91 | fileName = fileName.Substring(pos + 2); |
||
92 | } |
||
93 | |||
94 | return HttpUtility.UrlDecode(fileName, encoding); |
||
95 | } |
||
96 | |||
97 | } |
||
98 | } |