asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net下载文件几种方式


发布日期:2022年03月20日
 
asp.net下载文件几种方式
//TransmitFile实现下载

protectedvoid Button_Click(object sender EventArgs e)

{

/*

微软为Response对象提供了一个新的方法TransmitFile来解决使用ResponseBinaryWrite

下载超过mb的文件时导致Aspnet_wpexe进程回收而无法成功下载的问题

代码如下

*/

ResponseContentType =application/xzipcompressed;

ResponseAddHeader(ContentDisposition attachment;filename=zzip

string filename = ServerMapPath(DownLoad/zzip

ResponseTransmitFile(filename)

}

//WriteFile实现下载

protectedvoid Button_Click(object sender EventArgs e)

{

/*

using SystemIO;

*/

string fileName =asdtxt;//客户端保存的文件名

string filePath = ServerMapPath(DownLoad/aaatxt//路径

FileInfo fileInfo =new FileInfo(filePath)

ResponseClear()

ResponseClearContent()

ResponseClearHeaders()

ResponseAddHeader(ContentDisposition attachment;filename=+ fileName)

ResponseAddHeader(ContentLength fileInfoLengthToString())

ResponseAddHeader(ContentTransferEncoding binary

ResponseContentType =application/octetstream;

ResponseContentEncoding = SystemTextEncodingGetEncoding(gb

ResponseWriteFile(fileInfoFullName)

ResponseFlush()

ResponseEnd()

}

//WriteFile分块下载

protectedvoid Button_Click(object sender EventArgs e)

{

string fileName =aaatxt;//客户端保存的文件名

string filePath = ServerMapPath(DownLoad/aaatxt//路径

SystemIOFileInfo fileInfo =new SystemIOFileInfo(filePath)

if (fileInfoExists ==true)

{

constlong ChunkSize =;//K 每次读取文件只读取K这样可以缓解服务器的压力

byte[] buffer =newbyte[ChunkSize];

ResponseClear()

SystemIOFileStream iStream = SystemIOFileOpenRead(filePath)

long dataLengthToRead = iStreamLength;//获取下载的文件总大小

ResponseContentType =application/octetstream;

ResponseAddHeader(ContentDisposition attachment; filename=+ HttpUtilityUrlEncode(fileName))

while (dataLengthToRead >&& ResponseIsClientConnected)

{

int lengthRead = iStreamRead(buffer ConvertToInt(ChunkSize))//读取的大小

ResponseOutputStreamWrite(buffer lengthRead)

ResponseFlush()

dataLengthToRead = dataLengthToRead lengthRead;

}

ResponseClose()

}

}

//流方式下载

protectedvoid Button_Click(object sender EventArgs e)

{

string fileName =aaatxt;//客户端保存的文件名

string filePath = ServerMapPath(DownLoad/aaatxt//路径

//以字符流的形式下载文件

FileStream fs =new FileStream(filePath FileModeOpen)

byte[] bytes =newbyte[(int)fsLength];

fsRead(bytes bytesLength)

fsClose()

ResponseContentType =application/octetstream;

//通知浏览器下载文件而不是打开

ResponseAddHeader(ContentDisposition attachment; filename=+ HttpUtilityUrlEncode(fileName SystemTextEncodingUTF))

ResponseBinaryWrite(bytes)

ResponseFlush()

ResponseEnd()

}

//

publicvoid DownloadFile( SystemWebUIPage WebFormString FileNameWhenUserDownload String FileBody )

{

WebFormResponseClearHeaders()

WebFormResponseClear()

WebFormResponseExpires =;

WebFormResponseBuffer =true;

WebFormResponseAddHeader(AcceptLanguage zhtw

//文件名称

WebFormResponseAddHeader(contentdisposition attachment; filename=+SystemWebHttpUtilityUrlEncode(FileNameWhenUserDownload SystemTextEncodingUTF)+

WebFormResponseContentType =Application/octetstream;

//文件内容

WebFormResponseWrite(FileBody)//

WebFormResponseEnd()

}

//上面这段代码是下载一个动态产生的文本文件若这个文件已经存在于服务器端的实体路径则可以通过下面的函数

publicvoid DownloadFileByFilePath( SystemWebUIPage WebFormString FileNameWhenUserDownload String FilePath )

{

WebFormResponseClearHeaders()

WebFormResponseClear()

WebFormResponseExpires =;

WebFormResponseBuffer =true;

WebFormResponseAddHeader(AcceptLanguage zhtw

//文件名称

WebFormResponseAddHeader(contentdisposition attachment; filename=+ SystemWebHttpUtilityUrlEncode(FileNameWhenUserDownload SystemTextEncodingUTF) +

WebFormResponseContentType =Application/octetstream;

//文件内容

WebFormResponseWrite(SystemIOFileRea}dAllBytes(FilePath))//

WebFormResponseEnd()

}

               

上一篇:ASP.NET页面间数据传递的方法

下一篇:ASP.NET生成eurl.axd Http异常错误处理