프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.Extensions / Helper / GetDownloadFileName.cs @ 06f13e11

이력 | 보기 | 이력해설 | 다운로드 (2.6 KB)

1
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
        private static string GetDownloadFileName(string downloadUri)
43
        {
44
            string fileName = "";
45

    
46
            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

    
55
            return fileName;
56
        }
57

    
58
        private static string GetFileNameInDisposition(System.Net.Mime.ContentDisposition contentDisposition)
59
        {
60
            if (contentDisposition.FileName != null)
61
            {
62
                return contentDisposition.FileName;
63
            }
64

    
65
            var fileName = contentDisposition.Parameters["filename*"];
66
            if (fileName == null)
67
            {
68
                return null;
69
            }
70

    
71
            var pos = fileName.IndexOf("''", StringComparison.InvariantCulture);
72
            var encoding = Encoding.UTF8;
73
            if (pos >= 0)
74
            {
75
                try
76
                {
77
                    encoding = Encoding.GetEncoding(fileName.Substring(0, pos));
78
                }
79
                catch (ArgumentException)
80
                {
81
                }
82
                fileName = fileName.Substring(pos + 2);
83
            }
84

    
85
            return HttpUtility.UrlDecode(fileName, encoding);
86
        }
87

    
88
    }
89
}
클립보드 이미지 추가 (최대 크기: 500 MB)