This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
DownloadablePage.cs
82 lines (75 loc) · 3.52 KB
/
DownloadablePage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
namespace Mygod.Skylark
{
public class DownloadablePage : Page
{
protected void TransmitFile(string filePath, string fileName = null, string mime = null)
{
var fileInfo = new FileInfo(filePath);
long responseLength = fileInfo.Exists ? fileInfo.Length : 0, startIndex = 0;
var etag = '"' + HttpUtility.UrlEncode(filePath, Encoding.UTF8)
+ File.GetLastWriteTimeUtc(filePath).ToString("r") + '"';
// if the "If-Match" exists and is different to etag (or is equal to any "*" with no resource)
if (Request.Headers["If-Match"] == "*" && !fileInfo.Exists ||
Request.Headers["If-Match"] != null && Request.Headers["If-Match"] != "*"
&& Request.Headers["If-Match"] != etag)
{
Response.StatusCode = (int)HttpStatusCode.PreconditionFailed;
Response.End();
return;
}
if (!fileInfo.Exists)
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
Response.End();
return;
}
if (Request.Headers["If-None-Match"] == etag)
{
Response.StatusCode = (int)HttpStatusCode.NotModified;
Response.End();
return;
}
if (Request.Headers["Range"] != null
&& (Request.Headers["If-Range"] == null || Request.Headers["If-Range"] == etag))
{
var match = Regex.Match(Request.Headers["Range"], @"bytes=(\d*)-(\d*)");
startIndex = Parse<long>(match.Groups[1].Value);
responseLength = (Parse<long?>(match.Groups[2].Value) + 1 ?? fileInfo.Length) - startIndex;
Response.StatusCode = (int)HttpStatusCode.PartialContent;
Response.Headers["Content-Range"] = "bytes " + startIndex + "-" + (startIndex + responseLength - 1)
+ "/" + fileInfo.Length;
}
Response.Headers["Accept-Ranges"] = "bytes";
Response.Headers["Content-Length"] = responseLength.ToString(CultureInfo.InvariantCulture);
Response.Cache.SetCacheability(HttpCacheability.Public); //required for etag output
Response.Cache.SetETag(etag); //required for IE9 resumable downloads
Response.ContentType = mime ?? "application/octet-stream";
if (!string.IsNullOrEmpty(fileName)) Response.AddHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20"));
Response.TransmitFile(filePath, startIndex, responseLength);
}
private static T Parse<T>(object value)
{
//convert value to string to allow conversion from types like float to int
//converter.IsValid only works since .NET4 but still returns invalid values for a few cases like NULL
//for uint and not respecting locale for date validation
try
{
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value.ToString());
}
catch (Exception)
{
return default(T);
}
}
}
}