后台代码:

        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. C#TextBox自动滚动到最低端

    C#中一个RichTextBox,当新写入内容时,超过当前显示区域后,自动滚动到最低端,方便查看内容. private void txtInfo_TextChanged(object sender,  ...

  2. centos下安装nginx和php-fpm

    安装这两个花了大约七个小时,简直呵呵,安装nginx就是直接 yum install nginx ,但发现一打开php文件就是直接下载该php文件,也就是不能识别php文件,解决这个花了好久,但其实看 ...

  3. mysql优化技巧《转》

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  4. HDU5807分段dp

    DAG图. [题意] n(50)个城市m(c(n,2))条单向边(x,y),保证x<y 对于三个点(x,y,z)如果abs(w[x]-w[y])<=K && abs(w[x ...

  5. C# 上传RAR文件 解压 获取解压后的文件名称

    此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...

  6. MonkeyRunner学习(2)常用命令

    目录: 1.截图 2.暂停 (时延秒) 3.屏幕操作 4.打印 5.字符串发送到键盘输入(登录输入) 6.唤醒设备屏幕 7.重起手机 8.按键(系统键) 9.回车键 10.for 循环 11.循环截图 ...

  7. mysql 理解 int(11)

    1.这里的int(11) 与int的大小和存储字节,没有一毛钱关系,int的存储字节是4个字节,最大值为 65536*65536 = 40多亿,对于有符号的int,是20多亿.2.那么这里的(11) ...

  8. .net 连接sqlserver类库

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  9. (一)二维数组&&指针数组与数组指针

    一.首先我们从字面意思理解一下什么是指针数组什么是数组指针 1.指针数组:本质是一个数组,数组中的每一个元素是一个指针. 2.数组指针:本质是一个指针,而指针指向一个数组. 二.我们该怎么区分指针数组 ...

  10. android 主线程和子线程之间的消息传递

    从主线程发送消息到子线程(准确地说应该是非UI线程)  package com.zhuozhuo; import android.app.Activity; import android.os.Bun ...