WEB文件上传下载功能
WEB文件上传下载在日常工作中经常用到的功能
这里用到JS库
http://files.cnblogs.com/meilibao/ajaxupload.3.5.js
上传代码段(HTML)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UrlTest.aspx.cs" Inherits="WebDome.UrlTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title>
<script src="../../scripts/ajaxupload.3.5.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var btnUpload = $('#lblFile');
var status = $('#lblstatus');
new AjaxUpload(btnUpload, {
action: '/Upload.aspx',
name: 'txtFile',
onSubmit: function (file, ext) {
if (!(ext && /^(xls|doc|xlsx|docx|pdf|swf|zip|rar)$/.test(ext))) {
status.text('温馨提示:只能上传Excel、Word、PDF,ZIP,RAR或者SWF文件。');
return false;
}
status.text('正在上传,请稍候...');
},
onComplete: function (file, response) {
status.text('');
$("#hdFilePath").val('');
var c = response.substring(0, 2);
var t = response.substring(3);
if (c === "00") {
status.text('上传成功。文件名称:' + response.substring(70));
$("#hdFilePath").val(response.substring(15));
} else {
status.text(t);
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
---下面是Upload.aspx处理上传文件
protected void Page_Load(object sender, EventArgs e)
{
try
{
string sPath = "/UploadFile/" + DateTime.Now.ToString("yyyyMMdd") + @"\" + CurrentAdmin.OpId + @"\";
string path = Server.MapPath(sPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
HttpPostedFile hpfFile = Request.Files["txtFile"];
if (hpfFile.ContentLength == 0)
throw new Exception("文件大小为0字节,上传文件失败!");
string extendName = hpfFile.FileName.Substring(hpfFile.FileName.LastIndexOf("."));
string tempFileName = Guid.NewGuid().ToString() + "_" + hpfFile.FileName.Substring(0, hpfFile.FileName.LastIndexOf("."));
hpfFile.SaveAs(path + tempFileName + extendName);
Response.Write("00|" + sPath + tempFileName + extendName);
} catch (Exception ex)
{
Response.Write("02|" + ex.Message);
}
}
--上传文件END---
下载文件
页面HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileDownload.aspx.cs" Inherits="WebDome.FileDownload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div>
<asp:LinkButton ID="lkbtnDownload"
CommandArgument="/UploadFile/<%=fileName%>" runat="server"
style=" text-decoration:none;color:Black;" onclick="lkbtnDownload_Click">LinkButton</asp:LinkButton>
</div> </form> </body> </html>
---DownLoad--Method
public static void DownLoadFile(System.Web.UI.WebControls.LinkButton LinkButton1, System.Web.UI.Page page)
{
string filePath = page.Server.MapPath(LinkButton1.CommandArgument as string);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
string fileName = fileInfo.Name;
string fileextend = fileInfo.Extension;
string contentType = "";
if (fileextend == ".xls")
contentType = "application/vnd.ms-excel";
if (fileextend == ".xlsx")
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
if (fileextend == ".doc")
contentType = "application/msword";
if (fileextend == ".docx")
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
if (fileextend == ".pdf")
contentType = "application/pdf";
if (fileextend == ".swf")
contentType = "application/x-shockwave-flash";
page.Response.Clear();
page.Response.ClearContent();
page.Response.ClearHeaders();
page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName.Substring(37), System.Text.Encoding.UTF8));
page.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
page.Response.AddHeader("Content-Transfer-Encoding", "binary");
page.Response.ContentType = contentType;
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
page.Response.WriteFile(fileInfo.FullName);
page.Response.Flush();
page.Response.Close();
}
搞定--------------------
WEB文件上传下载功能的更多相关文章
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- JavaWeb实现文件上传下载功能实例解析 (好用)
转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...
- java web 文件上传下载
文件上传下载案例: 首先是此案例工程的目录结构:
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- SpringMVC整合fastdfs-client-java实现web文件上传下载
原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...
- web文件上传下载组件
最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
----------------------------------------------------------------------------------------------[版权申明: ...
- php实现文件上传下载功能小结
文件的上传与下载是项目中必不可少的模块,也是php最基础的模块之一,大多数php框架中都封装了关于上传和下载的功能,不过对于原生的上传下载还是需要了解一下的.基本思路是通过form表单post方式实现 ...
- Java web文件上传下载
[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52048666 作者:朱培 ID:sdksdk0 邮 ...
随机推荐
- Chrome 中的 JavaScript 断点设置和调试技巧 (转载)
原文地址:http://han.guokai.blog.163.com/blog/static/136718271201321402514114/ 你是怎么调试 JavaScript 程序的?最原始的 ...
- HTTP协议学习-02
HTTP 协议详解之 URL 的组成 http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于 TCP 的连接方式,HTTP1.1 版本中给出一种持续连接的机制,绝大多数的 ...
- FCKEditor使用说明
1.基本设置 先看看效果是什么样的: 效果图: 那么为什么说是FCKeditor的冰冷之心呢?这不是哗众取宠,主要是说它使用起来有点麻烦,下文就详细说明如何搞定这玩意儿. 1.FCKedito ...
- MVC中的Ajax(AjaxHelper)
authour: chenboyi updatetime: 2015-04-30 20:47:49 friendly link: 目录 1,思维导图 2,ActionLink() 3,BeginF ...
- python getpass模块:隐藏不显示输入的密码
不知道为什么,本机测试必须要在debug模式下才正常运行.. import getpass #用于隐藏用户输入的字符串,常用来接收密码 def checkuser(user,passwd): ': r ...
- Xcode7调试-b
Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. 1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BA ...
- nginx的使用配置
nginx为反向代理服务器,可以反向代理不同域名转向不同的具体服务器.可以用于负载压力或是同一台机器使用不同域名进行访问. 以下片段是服务器配置: #user cmcc; worker_process ...
- EPZS搜索过程
EPZS(Enhance Predictive Zonal Search) 增强预测区域搜索,是一种整像素运动估计的搜索算法. EPZS采用的是相关性较高的预测方法.这里的相关性较高是指,更多地根据已 ...
- 从MVC框架看MVC架构的设计(转)
尽管MVC早已不是什么新鲜话题了,但是从近些年一些优秀MVC框架的设计上,我们还是会发现MVC在架构设计上的一些新亮点.本文将对传统MVC架构中的一些弊病进行解读,了解一些优秀MVC框架是如何化解这些 ...
- Extjs4 中在指定光标处插入值
var rulearea = Ext.getCmp(文本域Id); var rulevalue = rulearea.getValue();// 获取文本textarea 里面的值 var start ...