[轉貼]ASP.NET File Downloading

來源:互聯網
上載者:User
原帖地址 http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/default.aspx

偶爾發現這篇文章,瀏覽了一下,把關鍵代碼COPY,留作紀念。

1.下載檔案而不是開啟檔案(Forcing a File Download Dialog):Setting the Content-Disposition Response HeaderSub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Response.Clear()
        Response.AddHeader("Content-Disposition", _
                           "attachment; filename=" & toDownload.Name)
        Response.AddHeader("Content-Length", _
                           file.Length.ToString())
        Response.ContentType = "application/octet-stream"
        Response.WriteFile(file.FullName)
        Response.End()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

2.分塊下載大檔案(Downloading Huge Files in Small Pieces):Writing File Chunks to the ClientSub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Const ChunkSize As Long = 10000
        Dim buffer(ChunkSize) As Byte
            
        Response.Clear()
        Using iStream As FileStream = File.OpenRead(path)
            Dim dataLengthToRead As Long = iStream.Length
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("Content-Disposition", _
                               "attachment; filename=" & toDownload.Name)
            While dataLengthToRead > 0 AndAlso Response.IsClientConnected
                Dim lengthRead As Integer = _
                    iStream.Read(buffer, 0, ChunkSize)
                Response.OutputStream.Write(buffer, 0, lengthRead)
                Response.Flush()
                dataLengthToRead = dataLengthToRead - lengthRead
            End While
        End Using
        Response.Close()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

3.解決用BinaryWrite或下載大檔案導致大記憶體丟失:Using TransmitFileSub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Response.Clear()
        Select Case System.IO.Path.GetExtension(strFileName)
            Case ".zip"
                Response.ContentType = "application/x-zip-compressed"
                Response.AddHeader("Content-Disposition", _
                    "attachment;filename=NEWDL_" + toDownload.Name)
                Response.TransmitFile(path)

            Case Else
               ‘ File Extension not supported.
        End Select
        Response.End()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.