在asp.net中使用jquery.uploadify-3.1实现上传

来源:互联网 发布:最优化教学 编辑:程序博客网 时间:2024/05/16 16:56
 using System;


jquery.uploadify-3.1是基于jquery的一款文件上传插件

插件下载地址:http://www.uploadify.com/wp-content/uploads/files/uploadify-v3.1.zip

文档地址:http://www.uploadify.com/documentation/

 

第一步:

创建一个asp.net网站或者web应用程序,并添加红框内的文件,其中uploadify-v3.1是下载的插件解压后直接拷贝到Script目录下的.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第二步:

创建一个httpHandler处理文件

 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;namespace WebApplication1.Handlers{/// <summary>/// UploadHandler 的摘要说明/// </summary>public class UploadHandler : IHttpHandler{public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";context.Response.Charset = "utf-8";HttpPostedFile file = context.Request.Files["Filedata"];string uploadPath =HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";if (file != null){if (!Directory.Exists(uploadPath)){Directory.CreateDirectory(uploadPath);}file.SaveAs(uploadPath + file.FileName);//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失context.Response.Write("1");}else{context.Response.Write("0");}}public bool IsReusable{get{return false;}}}}


第三步:

编写html代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadTest.aspx.cs" Inherits="WebApplication1.UploadTest" %><!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>    <link href="/Scripts/uploadify-v3.1/uploadify.css" rel="stylesheet" type="text/css" />    <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>    <script type="text/javascript" src="/Scripts/uploadify-v3.1/jquery.uploadify-3.1.min.js"></script>    <script type="text/javascript">        $(function () {            $('#upload').uploadify({                'formData': { 'folder': '/Uploads' },                'buttonText': '选择文件',                'buttonClass': 'browser',                'removeCompleted': false,                'swf': '/Scripts/uploadify-v3.1/uploadify.swf',                'debug': false,                'height': 15,                'width':70,                'uploader': '/Handlers/UploadHandler.ashx'            });        });    </script>    <style type="text/css">        .browser        {            color: White;        }    </style></head><body>    <form id="form1" runat="server">    <div>        <input type="file" name="upload" id="upload" />    </div>    </form></body></html>


第四步:完成查看效果

 

 

 

 

 

 

 

有人可能遇到以下问题:

?:上传大文件的IO Error问题:

解决办法:修改web.config

  <system.web>    <httpRuntime maxRequestLength="3097151" executionTimeout="50000"/>  </system.web>

?:500错误,可能是你的handler文件错误

 

为懒人附上源码吧(O(∩_∩)O~):

http://dl.dbank.com/c0g2mem8ho

原创粉丝点击