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. SQL Server分区动态生成脚本(三)(按年份划分)

    --生成分区脚本DECLARE @DataBaseName NVARCHAR(50)--数据库名称DECLARE @TableName NVARCHAR(50)--表名称DECLARE @Column ...

  2. Spring ApplicationContext的事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...

  3. PYTHON WEATHER

    小玩一下python强大的库文件,调api获取天气情况 #coding:utf-8 import urllib import json content = urllib.urlopen('http:/ ...

  4. sql server 国内外 2个同步 ,加一个表.加入同步种

    国内 和国外sql server 订阅 ,数据同步. 因为表是刚开始就弄好的. 那么如果国内加一个表.国外没法同步过去 步骤:1.国外也建一个一抹一样的表 步骤:2.把国内的数据导入到国外 步骤:3. ...

  5. zoj 3785 What day is that day?

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272 打表找规律. #include <cstdio> #incl ...

  6. static用法一

    #include "stdafx.h" #include "string.h" struct student { int num; ]; char sex; } ...

  7. (转载)PHP substr(),mb_substr()及mb_strcut的区别和用法

    (转载)http://blog.csdn.net/alongken2005/article/details/7098506 PHP substr()函数可以 分割文字,但要分割的文字如果包括中文字符往 ...

  8. 跨站脚本(XSS)

    跨站脚本: cross-site scripting或者XSS, 即攻击者向目标Web站点注入HTML标签或者脚本 如果网站没有通过移除任何嵌入的HTML标签来消毒,那么web页面很容易受到跨站脚本攻 ...

  9. Delphi TcxTreeList 读取 TcxImageComboBoxItem类型的值

    Delphi  TcxTreeList 读取  TcxImageComboBoxItem类型的值: Node.Values[wiNodeLevel.ItemIndex]://值 Node.Texts[ ...

  10. Java---软件试用次数(Properties类的简单使用)

    编程练习(软件试用次数) 实现一个如下的软件小功能: 记录软件运行的次数并在每次运行时提示已经运行的次数.如果运行次数大于5次,软件不再运行并给出提示:试用次数已到,请注册! 本代码只简单的介绍了软件 ...