首页 > 编程 > .NET > 正文

ASP.NET中实现直接从网页上下载文件

2024-07-10 13:07:42
字体:
来源:转载
供稿:网友

函数名: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 

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表