自己编写分封装类,大家帮我看一下,有错误还望指导
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); // 调用下载类下载
}
分享到:
相关推荐
C# ASP.net 通过压缩成zip文件,下载文件夹。占用内存少,支持空文件夹,中文名文件。
利用asp.net遍历选中文件夹,附带说明
【ASP.NET编程知识】asp.net遍历文件夹下所有子文件夹并绑定到gridview上的方法.docx
asp.net 遍历指定文件夹的所有文件(包括文件夹)
一段简单的Asp.net文件夹及文件的处理
NULL 博文链接:https://shirlly.iteye.com/blog/340007
Asp.Net文件和文件夹操作大全
c#源码 文件夹上传、修改、删除,文件格式限制,文件大小限制asp.net,适合初学者
ASP.NET Web API File Upload and Multipart MIME
资源名:ASP.NET仿百度网盘文件分享文件管理系统源码 资源类型:程序源代码 源码说明:创建无限级文件夹,上传文件,可进行删除,下载 分享等功能,异步上传 无限级文件夹:可在根目录中创建文件夹,点击文件夹即可...
【ASP.NET编程知识】ASP.NET访问共享文件夹的详细步骤.docx
asp.net删除指定文件夹下所有文件,(所有文件包括文件夹和文件)
ASP.NET 递归下载treeview
文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,本文详细介绍ASP.NET中文件上传下载方法集合 !
asp.net利用ftp上传文件实现示例代码 传统的asp.net上传会经常不稳定,不是出现超时就是大小限制 代码示例会帮到你
清理上传文件中未使用的垃圾附件。含详细代码注释
.net ftp上传文件文件夹,上传目录下的所有文件夹和文件
采用ASP.NET MVC 技术借助Easy-UI 前端框架 制作文件管理demo,文件操作包括上传,下载,复制,黏贴,压缩,重命名等。文件操作均对于实际文件夹,不包含数据库部分
ASP.NET 图片上传下载缩略察看C#和VB双版.rar 从本地向SQL Server上传存入图片 从SQL Server下载并在网页中显示图片 同时能显示缩略图和大图 含VB.NET和C#两个版本 内含数据库文件,简单附加到SQL Server中即可 ...
c# asp.net 读取项目中的文件,获取text里面定义的内容和值方法