zoukankan      html  css  js  c++  java
  • [转贴]ASP.NET File Downloading

    原帖地址 http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/default.aspx

    偶尔发现这篇文章,浏览了一下,把关键代码COPY,留作纪念。

    1.下载文件而不是打开文件(Forcing a File Download Dialog):Setting the Content-Disposition Response Header
    Sub Page_Load(ByVal sender As ObjectByVal 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 Client
    Sub Page_Load(ByVal sender As ObjectByVal 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 TransmitFile
    Sub Page_Load(ByVal sender As ObjectByVal 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


  • 相关阅读:
    JQuery 学习总结及实例 !! (转载)
    JavaScript 学习笔记
    个人对JS的一些见解
    本博客欢迎交流,文章自由转载,保持署名!
    VSCode:源码编译运行,分析,踩坑
    ant design pro/前端/JS:实现本地运行https
    前端/JS/React/ES6:纯前端实现图片压缩技术
    云服务名词:软件即服务SaaS,怎么这个理解起来这么别扭
    React:effect应该怎么翻译比较合适??
    我给博客加了一个娃娃,一片雪花
  • 原文地址:https://www.cnblogs.com/rock_chen/p/778591.html
Copyright © 2011-2022 走看看