使用ASP.NET临时文件下载链接下载链接、临时文件、ASP、NET

2023-09-03 00:36:58 作者:嘴唇ϟ愛上煙

我想知道我可以生成一个临时的下载地址为我的文件在有限的时间。我知道这是不是最好的做法,可能使用HttpHandlers的就是这样,根据去http://www.devx.com/$c$cmag/Article/34535/1954 不过我很好奇,想知道我可以使用URL重写使用GUID或其他一些神秘的命名方法来生成一个文件名,并使其可在有限的时间。 我倒是AP preciate如果有人点我一个很好的文章了。

I'd like to know how I can generate a temporary download address for my file for a limited time. I know that's not the best practice and probably using HttpHandlers is the way to go according to http://www.devx.com/codemag/Article/34535/1954 But I'm curious to know how I can use urlrewriting to generate a file name using a GUID or some other cryptic naming technique and make it available for a limited time. I'd appreciate if anyone points me a good article about it.

推荐答案

那么首先你需要某种形式的标识符。您能否提供一个GUID,而这很容易做到, Guid.NewGuid()。的ToString(N)为您提供了这样一个标识符。

Well first you need some form of identifier. You suggest a GUID and that's easily done, Guid.NewGuid().ToString("n") gives you such an identifier.

您谈谈URI重写的,但实际上只是一个有点抛光。你当然可以做一些重写打开 / MYFILES / a948ec43e5b743548fd9a77c462b953e /myFiles/download.aspx?id=a948ec43e5b743548fd9a77c462b953e 或即使进入 MYFILES / download.aspx?ID = 3 MYFILES / download.aspx?文件名= myNewDownload.pdf(检查看看表后) 。这是与任何其他URI重写任务,所以现在让我们忽略它,我们假定我们已经请求进入 /myFiles/download.aspx?id=a948ec43e5b743548fd9a77c462b953e 这是否是由于重写或不

You talk of URI rewriting, but that's really just a bit of polish. You could certainly do some rewriting to turn /myFiles/a948ec43e5b743548fd9a77c462b953e into /myFiles/download.aspx?id=a948ec43e5b743548fd9a77c462b953e or even (after checking a look up table) into myFiles/download.aspx?id=3 or myFiles/download.aspx?fileName=myNewDownload.pdf. This is the same as any other URI rewriting task, so for now lets just ignore it and assume we've a request coming into /myFiles/download.aspx?id=a948ec43e5b743548fd9a77c462b953e whether that is due to rewriting or not.

好。你有一个标识符,你需要匹配这三件事情:一个流,内容类型及使用期限

Okay. You've got an identifier, you need to match this to three things: a stream, a content type and an expiry date.

您可以存储所有这些文件系统中,所有的它在数据库或在数据库中的信息的路径,其中该流的形式存储在文件系统中的文件。

You could store all of this in the file system, all of it in a database or the details in the database including a path to where the stream is stored as a file in the filesystem.

让有相似的名字说,将其存储在文件系统:

Lets say store it in the file system with names like:

a948ec43e5b743548fd9a77c462b953e.application_pdf和a5d360178ec14e97abd556ed4b7709cf.text_plain;字符集= UTF-8

a948ec43e5b743548fd9a77c462b953e.application_pdf and a5d360178ec14e97abd556ed4b7709cf.text_plain;charset=utf-8

请注意,我们不使用普通的Windows文件扩展名,所以我们处理好与地方上载机有不同的绑定到你的服务器的情况。

Note that we aren't using normal windows file extensions, so we deal well with the case where the uploading machine had different bindings to your server.

在a948ec43e5b743548fd9a77c462b953e的情况下,我们先来看看创建日期,如果这是很久以前(文件已到期),我们会发送410 GONE头并显示一条错误消息,说明已经过期的文件被要求的项目(我们可以同时删除该文件在这一点上,清理使用 - 或者截断它,所以它仍然是该文件曾经存在的记录,但存储0bytes)

In the case of a948ec43e5b743548fd9a77c462b953e being the item required we first look at the creation date and if it's too long ago (the file has expired), we send a 410 GONE header with an error message explaining the file has expired (we can also delete the file at this point to clean up usage - or perhaps truncate it so it remains a record that the file used to exist, but is 0bytes of storage).

否则,我们设置 Response.ContentType 来应用程序/ PDF格式,然后 Response.TransmitFile 发送文件

Otherwise we set Response.ContentType to "application/pdf" and then Response.TransmitFile to send the file.

如果我们想要存储的数据流以不同的方式不是一个文件,我们会想它一小块一小块发(4096很好地匹配其他缓冲液系统的其他部分),并在它的情况下是非常大的叫 Response.Flush()定期prevent内存问题。

If we'd stored the stream a different way than as a file, we'd want to send it in small chunks (4096 nicely matches other buffers in other parts of the system) and in the case of it being very large call Response.Flush() periodically to prevent memory issues.

这是你做的基本制度。细微包括存储原始文件名,并在内容处理标头将其发送,并服从范围请求,以便用户可以恢复失败的下载,而不是必须从头开始。

That's your basic system done. Niceties would include storing the original file name and sending it in a content-disposition header, and obeying Range requests so that a user can resume a failed download rather than have to start from the beginning.

这一切是pretty的正交的使用,以确保只有正确的人的任何身份验证具有文件 - 你可以使用任何形式的登录系统使用它串联,或者你可以把它公开,但有时间限制

All of this is pretty orthogonal to any authentication used to ensure only the correct person has the file - you could use it in tandem with a login system of whatever sort, or you could leave it public but time-limited.