Response.Redirect的正在进行的下载 [英] Response.Redirect a download in progress

查看:134
本文介绍了Response.Redirect的正在进行的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户可以选择查看视频(无版权)这是一个第三方网站。从目前的用户选择了它,它需要被下载到我的Web服务器,然后用户可以访问它。我试图自动启动的第三方网站上下载,并与该路径的Response.Redirect的,但是当用户观看视频时,如果视频只有10秒的下载,任何球员/浏览器将considere这个视频作​​为十秒钟的视频和10秒后停止。

I have a website where a user can choose to view a video (no copyright) that is on a 3rd party website. From the moment the user choose it, it needs to be downloaded onto my web server before the user can access it. I tried to automatically start the download on the 3rd party website and make a response.redirect with this path, but when the user is watching the video, if the video has only 10 seconds downloaded, any player/browser will considere this video as a ten-seconds video and stop it after 10 seconds.

什么是重定向数据流而不是文件的最佳实践?

What would be the best practice to redirect a stream instead of a file?

推荐答案

我发现,这块code是做正是我想要的。它下载一个第三方网站的文件,并作为它的流式传输,每个块在响应writen使其完全obsfuscates的由来。因此,可以从任何网站上投放的任何文件,如果我拥有它。

I found that this piece of code is doing exactly what I want. It downloads a file on a third party website, and as it's streaming it, each chunk is writen in the Response so that it completely obsfuscates the origin. Thus, it is possible to serve any file from any website as if I own it.

Private Sub SendFile(ByVal url As String)
    Dim stream As System.IO.Stream = Nothing
    Dim bytesToRead As Integer = 10000
    Dim buffer() As Byte = New Byte((bytesToRead) - 1) {}

    Try

        Dim fileReq As System.Net.WebRequest = CType(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
        Dim fileResp As System.Net.HttpWebResponse = CType(fileReq.GetResponse, System.Net.HttpWebResponse)
        If (fileReq.ContentLength > 0) Then
            fileResp.ContentLength = fileReq.ContentLength
        End If
        stream = fileResp.GetResponseStream
        Dim resp As System.Web.HttpResponse = HttpContext.Current.Response
        resp.ContentType = "application/octet-stream"
        resp.AddHeader("Content-Disposition", ("attachment; filename=\""" + ("mp3" + "\""")))
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString)
        Dim length As Integer = 1000000
        While (length > 0)
            If resp.IsClientConnected Then
                length = stream.Read(buffer, 0, bytesToRead)
                resp.OutputStream.Write(buffer, 0, length)
                resp.Flush()
                buffer = New Byte((bytesToRead) - 1) {}
            Else
                length = -1
            End If        
        End While
    Catch
        stream.Close()
    Finally
        If (Not (stream) Is Nothing) Then
            stream.Close()
        End If
    End Try
    Response.End()
End Sub

这篇关于Response.Redirect的正在进行的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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