后台代码:

        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. Monocular Vision

    Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision:  ...

  2. OpenCV installation on Linux

    Getting the Cutting-edge OpenCV from the Git Repository Launch Git client and clone OpenCV repositor ...

  3. scala的传名参数

    scala如果你想实现像if ,while等类似的控制结构,根本没有值要传入花括号{}直接的代码里.要怎么实现. 比如说,我们要实现一个断言的控制结构,myAssert(), myAssert函数参考 ...

  4. I2总线

    I2C简介 飞利浦(PHILIPS)公司开发的两线式串行总线 每个器件都有一个唯一的地址识别 1.只要求两条总线线路:一条串行数据线SDA,一条串行时钟线SCL 2.每个连接到总线的器件都可以通过唯一 ...

  5. 关于Java控制台输入输出乱码问题

    产生原因:因为这个开源项目的默认字符编码为UTF-8,所以我的控制台的字符编码也自动变成了UTF-8,而键盘的输入流的默认格式是GBK格式,这样就造成了在GBK转UTF-8的过程中产生的奇数乱码错误( ...

  6. SAP研究贴之--发票校验提示移动平均价为负

    近日,应付岗密集出现发票校验时移动平均价为负值导致过账失败的情况,采购经理又是拍桌子.又是摔杯子的.财务经理安排任务彻底清查,找出问题原因.哎,毫无头绪啊...测试机模拟业务吧流程:合同(系统外)-采 ...

  7. Java 日期往后推迟n天

    在Java中经常会遇到,以指定的日期为基准,计算向后n天<n可以为正.负数,负数为向前移>. 这时需要在Date类的基础上使用Calendar类,处理方法基本如下: import java ...

  8. 强行替换exe图标的方法

    说句实话,要想用普通的方法来替换图标,不是完全不可行,当然也不是完全可行.这个看似简单的问题并不是想象中那么容易解决,为什么有人修改exe的图标总是失败,其实他忽视了exe和图标的复杂性,用简单的方法 ...

  9. css 常用样式命名规则

    大家在写css的时候,对一些html标签起一个合适的名字是个很头疼的事情,现在给大家分享项目中常用的名字供参考. 外套:wrap  ——用于最外层 头部:header  ——用于头部 主要内容:mai ...

  10. Vim优化

    写python代码时,希望缩进是4个空格,而不是制表符tab, 在vim中,我们只需要简单配置一下就ok了,打开~/.vimrc加上下面的几行(如果已经有了,修改一下数值就行了). set tabst ...