`
duoerbasilu
  • 浏览: 1482545 次
文章分类
社区版块
存档分类
最新评论

asp.net 从文件夹下载文件

 
阅读更多

自己编写分封装类,大家帮我看一下,有错误还望指导

1、简单的下载方法

protected void Button2_Click(object sender, EventArgs e)
{
String path = Server.MapPath("image/001.gif");
FileInfo fi = new FileInfo(path);
if (fi.Exists)
{
Response.AddHeader("Content-Disposition","attachment;filename="+fi.Name);
}
}

2、另一种下载方法

protected void Button2_Click(object sender, EventArgs e)
{
String filePath = Server.MapPath("image/001.gif");
String fileName = "001.gif";
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开 把attachment改为online 为在线砸开
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}

3、封装的下载类

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Xml.Linq;
public class down:System.Web.UI.Page
{
public down()
{
}
public void download(string fileName,String sql) //构造下载函数
{
string dbConnection = System.Configuration.ConfigurationSettings.AppSettings["ConStr"];
//连接数据库
SqlConnection myConnection = new SqlConnection(dbConnection);
//打开数据库
myConnection.Open();
//获取真实的名字
SqlCommand mySelect = new SqlCommand(sql, myConnection);
string fileNamels = mySelect.ExecuteScalar().ToString(); //只读取一个值
String filePath = System.Web.HttpContext.Current.Server.MapPath("../../FileSave" + "/" + Session["id"] + "/" + fileNamels);
//写入流
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开 把attachment改为online 为在线砸开
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}

调用方法:

点击gridview中的下载标识,下载文件

protected void ImageButton1_Click(object sender, ImageClickEventArgs e) //下载附件
{
ImageButton t = (ImageButton)sender;
int Index = ((GridViewRow)(t.NamingContainer)).RowIndex;//获得行号
String fileName = GridView2.Rows[Index].Cells[1].Text.ToString().Trim(); //获取要下载的文件的名字 filename
down dn = new down();
String sql = "SELECT afilename From tb_academic WHERE afilename2='" + fileName + "'"; //根据文件名字,获取数据库存储的真实文件的名字,文件夹中文件名字已经改动
dn.download(fileName,sql); // 调用下载类下载
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics