如何从安全位置 .NET 创建大型可断点下载

发布于 2024-08-14 02:58:26 字数 2320 浏览 10 评论 0原文

我需要先说明一下,我根本不是 .NET 编码员,但为了获得部分功能,我修改了一个 technet chunkedfilefetch.aspx 脚本,该脚本使用分块数据读取和写入流式文件传输方法,以帮助我完成任务。

iStream = New System.IO.FileStream(path, System.IO.FileMode.Open, _
    IO.FileAccess.Read, IO.FileShare.Read)
dataToRead = iStream.Length

Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Length", file.Length.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename=" & filedownload)

' Read and send the file 16,000 bytes at a time. '
While dataToRead > 0
    If Response.IsClientConnected Then
        length = iStream.Read(buffer, 0, 16000)
        Response.OutputStream.Write(buffer, 0, length)
        Response.Flush()
        ReDim buffer(16000) ' Clear the buffer '
        dataToRead = dataToRead - length
    Else
        ' Prevent infinite loop if user disconnects '
        dataToRead = -1
    End If
End While

这对于最大 2GB 的文件非常有效,并且现在可以完全正常工作。但只有一个问题是它不允许恢复。

我将原始代码命名为 fetch.aspx 并通过 URL 传递 orderNUM。 fetch.aspx&ordernum=xxxxxxx 然后,它根据订单号从数据库中读取文件名/位置,并从不在 Webroot 下的安全位置将其分块。

我需要一种方法来使其可恢复,由于互联网和大文件的性质,人们总是会断开连接,并希望从中断的地方恢复。但是我读过的任何可恢复的文章都假设该文件位于 webroot 中..即。 http://www.devx.com/dotnet/Article/22533/1954 很棒的文章,效果很好,但我需要从安全位置进行流式传输。

我根本不是 .NET 编码员,最多我可以做一些冷融合,如果有人可以帮助我修改处理程序来执行此操作,我将非常感激。

要求:

  • 我有一个工作正常的 fetch.aspx 脚本,该脚本运行良好,并使用上述代码片段作为流式下载的基础。
  • 下载文件大小为 600MB,并存储在 Webroot 之外的安全位置。
  • 用户单击 fetch.aspx 开始下载,因此如果失败,将再次单击它。如果分机是 .ASPX 并且发送的文件是 AVI,单击它将完全绕过映射到 .AVI 分机的 IHTTP 处理程序,因此这让我感到困惑
  • 根据我的理解,浏览器将读取并匹配 etag 值和修改的文件date 来确定它们正在谈论同一个文件,然后在浏览器和 IIS 之间交换后续的接受范围。由于这个对话框发生在 IIS 上,我们需要使用一个处理程序来拦截并相应地响应,但是单击链接会将其发送到一个 ASPX 文件,该处理程序需要位于 AVI 文件上。这也让我感到困惑。
  • 如果有一种方法可以请求包含 etag、accept-range 的初始 HTTP 请求标头到正常的 .ASPX 文件中,我可以读取这些值,如果 Accept-range 和 etag 存在,则以某种方式开始对该字节值进行分块?但我找不到一种方法来传输 http 请求标头,因为它们似乎在 IIS 级别丢失了。

  • URL字符串中传递的OrderNum是唯一的,可以用作ETag

     Response.AddHeader("ETag", request("ordernum"))
    
  • 由于大小,文件需要可恢复和分块。

  • 文件扩展名是 .AVI,因此可以围绕它编写处理程序。
  • IIS 6.0 Web 服务器

任何帮助都将不胜感激,我一直在阅读和下载代码,但是给出的示例都不符合我的情况,原始文件是从 webroot 外部流式传输的。请帮助我处理这些 httphandlers :)

I need to preface I'm not a .NET coder at all, but to get partial functionality, I modified a technet chunkedfilefetch.aspx script that uses chunked Data Reading and writing Streamed method of doing file transfer, to get me half-way.

iStream = New System.IO.FileStream(path, System.IO.FileMode.Open, _
    IO.FileAccess.Read, IO.FileShare.Read)
dataToRead = iStream.Length

Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Length", file.Length.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename=" & filedownload)

' Read and send the file 16,000 bytes at a time. '
While dataToRead > 0
    If Response.IsClientConnected Then
        length = iStream.Read(buffer, 0, 16000)
        Response.OutputStream.Write(buffer, 0, length)
        Response.Flush()
        ReDim buffer(16000) ' Clear the buffer '
        dataToRead = dataToRead - length
    Else
        ' Prevent infinite loop if user disconnects '
        dataToRead = -1
    End If
End While

This works great on files up to 2GB and is fully functioning now.. But only one problem it doesn't allow for resume.

I took the original code called it fetch.aspx and pass an orderNUM through the URL. fetch.aspx&ordernum=xxxxxxx It then reads the filename/location from the database occording to the ordernumber, and chunks it out from a secured location NOT under the webroot.

I need a way to make this resumable, by the nature of the internet and large files people always get disconnected and would like to resume where they left off. But any resumable articles i've read, assume the file is within the webroot.. ie. http://www.devx.com/dotnet/Article/22533/1954 Great article and works well, but I need to stream from a secured location.

I'm not a .NET coder at all, at best i can do a bit of coldfusion, if anyone could help me modify a handler to do this, i would really appreciate it.

Requirements:

  • I Have a working fetch.aspx script that functions well and uses the above code snippet as a base for the streamed downloading.
  • Download files are large 600MB and are stored in a secured location outside of the webroot.
  • Users click on the fetch.aspx to start the download, and would therefore be clicking it again if it was to fail. If the ext is a .ASPX and the file being sent is a AVI, clicking on it would completely bypass an IHTTP handler mapped to .AVI ext, so this confuses me
  • From what I understand the browser will read and match etag value and file modified date to determine they are talking about the same file, then a subsequent accept-range is exchanged between the browser and IIS. Since this dialog happens with IIS, we need to use a handler to intercept and respond accordingly, but clicking on the link would send it to an ASPX file which the handeler needs to be on an AVI fiel.. Also confusing me.
  • If there is a way to request the initial HTTP request header containing etag, accept-range into the normal .ASPX file, i could read those values and if the accept-range and etag exist, start chunking at that byte value somehow? but I couldn't find a way to transfer the http request headers since they seem to get lost at the IIS level.

  • OrderNum which is passed in the URL string is unique and could be used as the ETag

        Response.AddHeader("ETag", request("ordernum"))
    
  • Files need to be resumable and chunked out due to size.

  • File extensions are .AVI so a handler could be written around it.
  • IIS 6.0 Web Server

Any help would really be appreciated, i've been reading and reading and downloading code, but none of the examples given meet my situation with the original file being streamed from outside of the webroot. Please help me get a handle on these httphandlers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文