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 邮 ...
随机推荐
- Solr4.8.0源码分析(6)之非排序查询
Solr4.8.0源码分析(6)之非排序查询 上篇文章简单介绍了Solr的查询流程,本文开始将详细介绍下查询的细节.查询主要分为排序查询和非排序查询,由于两者走的是两个分支,所以本文先介绍下非排序的查 ...
- 14.4.1 Buffer Pool
14.4.1 Buffer Pool buffer pool 是一个主人的内存区域 InnoDB caches 表和index 数据. buffer pool 允许经常访问的数据直接从内存里处理,加快 ...
- 【HDOJ】1716 排列2
STL. /* 1716 */ #include <iostream> #include <algorithm> #include <cstdio> #includ ...
- Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so
Linux下的库操作工具-nm.ar.ldd.ldconfig和ld.so .nm [options] file 列出file中的所有符号 [option] -c 将符号转化为用户级的名字 -s 当用 ...
- web references是在.NET下的一个东东?它有什么用呢?和“引用”有什么区别!
WEB引用的意思啊 在.net中有类库和WEB SERVICE这两种类型的项目, 前者编译出来的DLL就是我们普通使用的引用中的类库, 后都编译出来的,在服务器IIS上为其提供服务,我们调用时就要用到 ...
- 如何创建WIN服务
sc create ServiceName binPath= "XXXX.exe" displayName= "中文xxxx"binpath和displayna ...
- python:文本文件处理
# coding=utf-8 #将列表写入文件 :'w+'(覆盖原有文件内容),'a+'(在原文件的基础上追加) def write_list_test(path,savelist,pattarn): ...
- C++ —— 笔记汇总
导读 本文仅用于记录在个人在使用C++过程中的遇到一些的疑问和概念. 目录 语法和概念基础 常用函数 编程注意 编译问题 拓展链接 1.语法和概念基础 1.块域 2.static 作用域 ...
- 部分实用的SQL语句
一.在数据库创建表格的SQL语句 1,创建一个link表格,包含属性:lid 主键,title 标题, imgpath 图片地址 , url 网址 , info 说明, isshow 显示1 ...
- Code Snippet Library
你可以将自己常用的代码放到里面,给它命名,设置快捷键,以后想用这段代码的时候只要按快捷键,就会出现提示,直接将这段代码显示出来,十分高效. 比如我经常会用到一个动画:[UIView beginAnim ...