当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET实现推送文件到浏览器的方法

ASP.NET实现推送文件到浏览器的方法

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

the rose shop,语文教学反思案例,浙江省瑞安市仙降镇

本文实例讲述了asp.net实现推送文件到浏览器的方法。分享给大家供大家参考。具体分析如下:

这里主要实现从服务器到浏览器,推送文件,提供用户下载/浏览的功能。

提示: 在ajax updatepanel里面将无效。如果代码从按钮单击事件中被调用,该按钮需要在 ajax updatepanel的外部。

具体代码如下:

/// <summary>
/// downloads (pushes) file to the client browser. 
/// **** note **** cannot be done from inside an ajax updatepanel control.
/// </summary>
/// <param name="fullfilepath">the full file path of the file</param>
protected void downloadfile(string fullfilepath)
{
  // gets the file name
  string filename = fullfilepath.substring(fullfilepath.lastindexof('\\') + 1);
  byte[] buffer;
  using (filestream filestream = new filestream(fullfilepath, filemode.open))
  {
    int filesize = (int)filestream.length;
    buffer = new byte[filesize];
    // read file into buffer
    filestream.read(buffer, 0, (int)filesize);
  }
  response.clear();
  response.buffer = true;
  response.bufferoutput = true;
  response.contenttype = "application/x-download";
  response.addheader("content-disposition", "attachment; filename=" + filename);
  response.cachecontrol = "public";
  // writes buffer to outputstream
  response.outputstream.write(buffer, 0, buffer.length);
  response.end();
}

希望本文所述对大家的asp.net程序设计有所帮助。

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

相关文章:

验证码:
移动技术网