zoukankan      html  css  js  c++  java
  • ASP.NET中实现直接从网页上下载文件

    函数名:ResponseFile
    功能  :客户端从服务器端下载一个文件
    返回值:返回True表示服务器响应成功,返回False表示失败
    参数  :
            PageResponse       响应客户端的Response对象,用Page.Response引用
            DownloadFileName   客户端下载文件的文件名
            LocalFilePath      服务器端待下载文件的路径
            DownloadBuffer     服务器端读取文件的缓冲区大小,单位为KB


    Public Function ResponseFile(ByRef PageResponse As HttpResponse, ByVal DownloadFileName As String, ByVal LocalFilePath As String, ByVal DownloadBuffer As Long) As Boolean
            Dim Reader As System.IO.FileStream
            Dim Buffer() As Byte
            Dim FileLength As Long
            Dim FileBuffer As Long = 1024 * DownloadBuffer
            Dim ReadCount As Long
            ReadCount = FileBuffer
            ReDim Buffer(ReadCount - 1)
            Try
                    Reader = System.IO.File.OpenRead(LocalFilePath)
                    FileLength = Reader.Length
                    Try
                            PageResponse.Buffer = False
                            PageResponse.AddHeader("Connection", "Keep-Alive")
                            PageResponse.ContentType = "application/octet-stream"
                            PageResponse.AddHeader("Content-Disposition", "attachment;filename=" + DownloadFileName)
                            PageResponse.AddHeader("Content-Length", FileLength.ToString)
                            While ReadCount = FileBuffer
                                    ReadCount = Reader.Read(Buffer, 0, FileBuffer)
                                    ReDim Preserve Buffer(ReadCount - 1)
                                    PageResponse.BinaryWrite(Buffer)
                            End While
                            Response.End()
                    Catch ex As Exception
                            Return False
                    Finally
                            Reader.Close()
                    End Try
            Catch ex As Exception
                    Return False
            End Try
            Return True
    End Function 

  • 相关阅读:
    搭建angularjs API文档站点
    Web网站数据”实时”更新设计
    3kb jQuery代码搞定各种树形选择。
    阿里妈妈自动登录程序
    Android中RelativeLayout属性详细说明
    jQuery选择器总结
    ajax页面加载进度条插件
    jquery ajax 方法及各参数详解
    使用jQuery加载html页面到指定的div
    C#中无边框窗体移动
  • 原文地址:https://www.cnblogs.com/nianshi/p/794407.html
Copyright © 2011-2022 走看看