创建临时链接进行下载 [英] Create temporary link for download

查看:445
本文介绍了创建临时链接进行下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET

我需要给用户从服务器下载文件的临时链接。

它应该是一个临时链接(页面),可用于短时间(例如12小时)。如何生成这个链接(或链接的临时网页)?

解决方案

这是一个相当完整的例子。

首先创建一个使用秘密盐加上一个到期时间的短十六进制字符串的功能:

  public static string MakeExpiryHash(DateTime expiry)
{
const string salt =some random bytes;
byte [] bytes = Encoding.UTF8.GetBytes(salt + expiry.ToString(s));
使用(var sha = System.Security.Cryptography.SHA1.Create())
返回string.Concat(sha.ComputeHash(bytes).Select(b => b.T​​oString(x2 )))子串(8)。
}

然后一个生成一周到期的链接的代码片段:

  DateTime expires = DateTime.Now + TimeSpan.FromDays(7); 
string hash = MakeExpiryHash(expires);
string link = string.Format(http:// myhost / Download?exp = {0}& k = {1},expires.ToString(s),hash);

最后,如果发送了有效的链接,发送文件的下载页面:

  DateTime expires = DateTime.Parse(Request.Params [exp]); 
string hash = MakeExpiryHash(expires);
if(Request.Params [k] == hash)
{
if(expires< DateTime.UtcNow)
{
//链接已过期
}
else
{
string filename =<文件路径>;
FileInfo fi = new FileInfo(Server.MapPath(filename));
Response.ContentType =application / octet-stream;
Response.AddHeader(Content-Disposition,attachment; filename =+ filename);
Response.AddHeader(Content-Length,fi.Length.ToString());
Response.WriteFile(fi.FullName);
Response.Flush();
}
}
else
{
//无效链接
}

您应该一定要包装一些异常处理来捕获被破坏的请求。


I use ASP.NET
I need to give user temporary link for downloading file from server.
It should be a temporary link (page), which is available for a short time (12 hours for example). How can I generate this link (or temporary web page with link)?

解决方案

Here's a reasonably complete example.

First a function to create a short hex string using a secret salt plus an expiry time:

public static string MakeExpiryHash(DateTime expiry)
{
    const string salt = "some random bytes";
    byte[] bytes = Encoding.UTF8.GetBytes(salt + expiry.ToString("s"));
    using (var sha = System.Security.Cryptography.SHA1.Create())
        return string.Concat(sha.ComputeHash(bytes).Select(b => b.ToString("x2"))).Substring(8);
}

Then a snippet that generates a link with a one week expiry:

DateTime expires = DateTime.Now + TimeSpan.FromDays(7);
string hash = MakeExpiryHash(expires);
string link = string.Format("http://myhost/Download?exp={0}&k={1}", expires.ToString("s"), hash);

Finally the download page for sending a file if a valid link was given:

DateTime expires = DateTime.Parse(Request.Params["exp"]);
string hash = MakeExpiryHash(expires);
if (Request.Params["k"] == hash)
{
    if (expires < DateTime.UtcNow)
    {
        // Link has expired
    }
    else
    {
        string filename = "<Path to file>";
        FileInfo fi = new FileInfo(Server.MapPath(filename));
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
        Response.AddHeader("Content-Length", fi.Length.ToString());
        Response.WriteFile(fi.FullName);
        Response.Flush();
    }
}
else
{
    // Invalid link
}

Which you should certainly wrap in some exception handling to catch mangled requests.

这篇关于创建临时链接进行下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆