当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 文件下载功能函数代码整理

asp.net 文件下载功能函数代码整理

2017年12月12日  | 移动技术网IT编程  | 我要评论

杜达雄2013灰色空间,pt电子89168澳门官方,昆仑决20160626

复制代码 代码如下:

public void filedownloaddel(string fullfilename)
{
system.io.stream istream = null;
// buffer to read 10k bytes in chunk:
byte[] buffer = new byte[10000];
// length of the file:
int length;
// total bytes to read:
long datatoread;
// identify the file to download including its path.
string filepath = fullfilename;
filepath = server.mappath(filepath);
// identify the file name.
string filename = system.io.path.getfilename(filepath);

try
{
// open the file.
istream = new system.io.filestream(filepath, system.io.filemode.open,
system.io.fileaccess.read, system.io.fileshare.read);

// total bytes to read:
datatoread = istream.length;
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachment; filename=" + filename);
// read the bytes.
while (datatoread > 0)
{
// verify that the client is connected.
if (response.isclientconnected)
{
// read the data in buffer.
length = istream.read(buffer, 0, 10000);
// write the data to the current output stream.
response.outputstream.write(buffer, 0, length);
// flush the data to the html output.
response.flush();
buffer = new byte[10000];
datatoread = datatoread - length;
}
else
{
//prevent infinite loop if user disconnects
datatoread = -1;
response.clear();
}
}
response.end(); //没有这句会将该页面刷新后的内容追加写入文件中。
}
catch (exception ex)
{
// trap the error, if any.
//response.write("error : " + ex.message);
//base.writelog("资料", "下载资料:" + ex.message + "!", logtype.error, this.gettype().tostring());
}
finally
{
if (istream != null)
{
//close the file.
istream.close();
}
file.delete(fullfilename);
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网