后台代码:

        public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass";
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
} protected void BtnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string name = FileUpload1.FileName;
string filepath = Server.MapPath("~/") + "upload\\" + name;
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = string.Format("insert into PathTable (Name,Path) values('{0}','{1}')",name,filepath);
int count = comm.ExecuteNonQuery();
if (count > )
{
FileUpload1.SaveAs(filepath);
LoadData();
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功')</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('保存失败')</script>");
}
}
}
} public void LoadData()
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select * from PathTable";
comm.ExecuteScalar();
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds.Tables[];
GridView1.DataBind();
}
} public static void DownloadFile(string physicalFilePath)
{
FileStream stream = null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize]; int bytesRead = stream.Read(buf, , bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(physicalFilePath));
HttpContext.Current.Response.OutputStream.Write(buf, , bytesRead);
HttpContext.Current.Response.End();
}
finally
{
stream.Close();
}
} protected void LinkButton_Click(object sender, CommandEventArgs e)
{
if (e.CommandArgument!=null)
{
string path = e.CommandArgument.ToString();
DownloadFile(path);
}
}

前台代码:

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:Label ID="Id" Text='<%#Eval("Id") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="文件名">
<ItemTemplate>
<asp:Label ID="Name" Text='<%#Eval("Name") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="路径">
<ItemTemplate>
<asp:Label ID="Path" Text='<%#Eval("Path") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="下载">
<ItemTemplate>
<asp:LinkButton Text="下载" runat="server" CommandArgument='<%#Eval("Path")%>' OnCommand="LinkButton_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:FileUpload ID="FileUpload1" runat="server"/>
</p>
<p>
<asp:Button ID="BtnSave" runat="server" Text="保存" OnClick="BtnSave_Click" />
</p>
</form>
</body>

注意:

1、GridView列中的TemplateField属性很实用,可以在其中添加其他发服务器控件、绑定事件和进行字段转换等;

2、设置CommandArgumet和OnCommand事件,可以在后台方便的获取绑定的字段;

3、Text='<%#Eval("Id")%>'   单引号内放双引号。

FileUpload上传与下载的更多相关文章

  1. day24(JAVAWEB上传与下载)

    javaWeb上传与下载 上传: 上传方式: jspSmartUpload   :应用在jsp上的文件上传与下载组件. FileUpload            :用用在jaava环境上的上传的功能 ...

  2. Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览

    1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示 ...

  3. java web学习总结(二十四) -------------------Servlet文件上传和下载的实现

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  4. Struts2入门(七)——Struts2的文件上传和下载

    一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...

  5. (转载)JavaWeb学习总结(五十)——文件上传和下载

    源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...

  6. JavaWeb学习总结,文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  7. java文件上传和下载

    简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...

  8. JavaWeb学习总结(五十)——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  9. 文件上传和下载(可批量上传)——Spring(三)

    在文件上传和下载(可批量上传)——Spring(二)的基础上,发现了文件下载时,只有在Chrome浏览器下文件名正常显示,还有发布到服务器后,不能上传到指定的文件夹目录,如上传20160310.txt ...

随机推荐

  1. nancy的诊断2

    接上文. 一 再看看第二个面板内容 点击带文号 的  Interactive Diagnostics 这个面板 如下 上面的是诊断程序,默认是系统提供的测试用的诊断程序.  你可以 通过实现IDiag ...

  2. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  3. 表单美化-原生javascript和jQuery多选按钮(兼容IE6)

    前些天我们讲了下单选按钮的美化今天来做表单元素多选按钮的美化.我们的想法是:利用多选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定. 用到的图 ...

  4. Linux设备模型(总线、设备、驱动程序和类)

    Linux设备驱动程序学习(13) -Linux设备模型(总线.设备.驱动程序和类)[转] 文章的例子和实验使用<LDD3>所配的lddbus模块(稍作修改). 提示:在学习这部分内容是一 ...

  5. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  6. JQuery $()后面的括号里的内容什么时候加引号,什么时候不加

    一.如果是已经声明存在的变量或者对象,就不用加引号. 比如var name=document.getElementById("name"); $(name)或者$(this). 二 ...

  7. [css] 浏览器字体和css设置字体之间的关系

    原文链接:http://www.zhangxinxu.com/wordpress/2010/06/%E5%8F%AF%E7%94%A8%E6%80%A7%E4%B9%8B%E6%B5%8F%E8%A7 ...

  8. MyBatis学习笔记(二) 关联关系

    首先给大家推荐几个网页: http://blog.csdn.net/isea533/article/category/2092001 没事看看 - MyBatis工具:www.mybatis.tk h ...

  9. svn 合并分支 等

    [转载]svn分支(branch)创建.合并(到trunk).冲突解决. Leave a reply 转载自:http://zccst.iteye.com/blog/1430823 一.创建分支 1, ...

  10. mongodb数据库js查询

    #健康风险-disease db.disease.find({versions:'2'}).forEach(function(item){ item.diseaseDetail && ...