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. 扩展PHP内置的异常处理类

    在try代码块中,需要使用throw语句抛出一个异常对象,才能跳到转到catch代码块中执行,并在catch代码块中捕获并使用这个异常类的对象.虽然在PHP中提供的内置异常处理类Exception,已 ...

  2. jquery 操纵 cookies 插件(1)

    当你浏览某网站时,你硬盘上会生产一个非常小的文本文件,它可以记录你的用户ID.密码.浏览过的网页.停留的时间等信息. 当你再次来到该网站时,网站通过读取Cookies,得知你的相关信息,就可以做出相应 ...

  3. matplotlib curve.py

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) sinX = np.sin(x) ...

  4. how to count uv area

    先放着,空了再整理.... fn getModeUvVolumetric mode chang= ----得到UV使用率( --global facesNumSum = meshop.getnumfa ...

  5. Solr4.8.0源码分析(14)之SolrCloud索引深入(1)

    Solr4.8.0源码分析(14) 之 SolrCloud索引深入(1) 上一章节<Solr In Action 笔记(4) 之 SolrCloud分布式索引基础>简要学习了SolrClo ...

  6. SQLServer 取小时

    select datepart(hh,getdate())--orselect datename(hh,getdate())

  7. 直接引用windows命名空间

    再逛博客的时候,看见一段这样代码.获取系统密码的 static void DumpCredentials(Windows.Security.Credentials.PasswordCredential ...

  8. Java中RMI框架

    嘎嘎,有空写……先记着了

  9. 你以为PHP那么好自定义升级?

    X,PHP如果只是安装操作系统之后,YUM INSTALL之后就可以使用最好啦. 但如果YUM安装的官方PHP版本太低怎么办? 据我所知,现在也只是到PHP5.3.3版本,如果APP应用需要PHP5. ...

  10. 配置Session变量的生命周期

    在Web.config文件中配置Session变量的生命周期是在<sessionState></sessionState>节中完成的,在配置Session的生命周期时,可以设置 ...