FileUpload上传与下载
后台代码:
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上传与下载的更多相关文章
- day24(JAVAWEB上传与下载)
javaWeb上传与下载 上传: 上传方式: jspSmartUpload :应用在jsp上的文件上传与下载组件. FileUpload :用用在jaava环境上的上传的功能 ...
- Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览
1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示 ...
- java web学习总结(二十四) -------------------Servlet文件上传和下载的实现
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- Struts2入门(七)——Struts2的文件上传和下载
一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...
- (转载)JavaWeb学习总结(五十)——文件上传和下载
源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...
- JavaWeb学习总结,文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- java文件上传和下载
简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- JavaWeb学习总结(五十)——文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- 文件上传和下载(可批量上传)——Spring(三)
在文件上传和下载(可批量上传)——Spring(二)的基础上,发现了文件下载时,只有在Chrome浏览器下文件名正常显示,还有发布到服务器后,不能上传到指定的文件夹目录,如上传20160310.txt ...
随机推荐
- FormsAuthentication.HashPasswordForStoringInConfigFile 方法 之研究
摘自:http://time-is-life.cnblogs.com/articles/322523.html 给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码. [C#] ...
- ubuntu使用mailx利用SMTP发送邮件
转载:http://www.blogjava.net/jasmine214--love/archive/2010/10/09/334102.htmlLinux下mail利用外部邮箱发送邮件的方法: 1 ...
- [转] Git SSH Key 生成步骤
Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. github的SSH配置如下: 一 . 设置Git的user name和email: $ git ...
- ZOJ-2562 More Divisors 反素数
题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最大的一个. 分析:反素数定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4 ...
- Redis实践操作之—— keyspace notification(键空间通知)
一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...
- 构建maven项目3
1.1.创建Jave Project 1.使用mvn archetype:generate命令,如下所示: mvn archetype:generate -DgroupId=com.mycompany ...
- git本地文件回滚操作
今天有几个文件改在了其他分支上.需要回滚. 参考了下面两篇文章: Link Link 简单讲,分多个不同的阶段: 1. 用git status命令看,发现是unstaged,那么就是只在work ...
- Object Pascal 面向对象的特性
2 面向对象的特性 在软件系统开发过程中,结构分析技术和结构设计技术具有很多优点,但同时也存在着许多难以克服的缺点.因为结构分析技术和结构设计技术是围绕着实现处理功能来构造系统的,而在系统维护和软件升 ...
- Oracle 删除重复的记录,只保留一条
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 g ...
- ios中javascript直接调用oc代码而非通过改变url回调方式(转)
之前一个ios项目中,需要通过UIWebview来打开一个静态页面,并在静态页面中 调用相关object-c代码. 一.以前使用js调用object-c的方法 关于如何使用javascript调用ob ...