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>

&nbsp;<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文件上传下载功能的更多相关文章

  1. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  2. JavaWeb实现文件上传下载功能实例解析 (好用)

    转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...

  3. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

  4. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  5. SpringMVC整合fastdfs-client-java实现web文件上传下载

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...

  6. web文件上传下载组件

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  7. 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现

    ----------------------------------------------------------------------------------------------[版权申明: ...

  8. php实现文件上传下载功能小结

    文件的上传与下载是项目中必不可少的模块,也是php最基础的模块之一,大多数php框架中都封装了关于上传和下载的功能,不过对于原生的上传下载还是需要了解一下的.基本思路是通过form表单post方式实现 ...

  9. Java web文件上传下载

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52048666 作者:朱培 ID:sdksdk0 邮 ...

随机推荐

  1. thinkphp框架之模型(数据库查询)

    1. 模型定义 文件名称必须是 表名+Model.class.php 例如:UserModel.class.php namespace Home\Model; //该模型类的命名空间 use Thin ...

  2. MongoDB-性能优化之索引

    首先看一个实例 >;i<;i++){ db.indexdemo.insert({),"create":new Date});} WriteResult({ }) > ...

  3. django自带User管理中添加自己的字段方法

    #coding=utf-8 from django.db import models from django.contrib.auth.models import User, make_passwor ...

  4. Egret 事件机制

    主要流程: private createGameScene():void { var JimGreen = new Boy(); var HanMeimei = new Girl(); JimGree ...

  5. PHP之intval()

    Title:PHP之intval()  --2014-02-26 13:57 <?php ...... $a='0x2720616e6420313d3220756e696f6e2073656c6 ...

  6. ISO14443-4块传输协议的实现

    ISO1444-3块传输协议主要用于应用数据的传输,其实现如下: unsigned char Apdu(unsigned char *comm,unsigned short len,unsigned ...

  7. 安全控件开发原理分析 支付宝安全控件开发 C++

    浏览器安全控件是如果支付宝一样结合web程序密码数据安全处理的程序,采用C++语言开发 通常的安全控件分为两种,一种是指支持IE内核的浏览器,一种支持所有内核的浏览器,支付宝采用的是支持所有内核的浏览 ...

  8. selenium webdriver python 操作浏览器

    新建driver driver=webdriver.Firefox() driver=webdriver.Ie() driver=webdriver.Chrome()   打开一个链接 driver. ...

  9. python用parammiko模块实现linux的远程操作

    parammiko  可以实现远程的带密码登录,解决ssh远程登陆需要交互的问题 (当然很多其他的,如tcl也可以).但这个用python做比较简单 1.parammiko 的安装 1.1.依赖模块 ...

  10. windows快捷键和命令

    以管理员方式打开命令行界面:win+X+A 打开服务界面:services.msc 删掉windows系统记住的WIFI密码 cmd下面运行 显示存储的无线连接netsh wlan show prof ...