Uploadify 3.2上传文件,限制类型,大小,传递参数等

来源:互联网 发布:新生入学调查问卷知乎 编辑:程序博客网 时间:2024/05/22 20:45
[csharp] view plain copy
 print?
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upload.aspx.cs" Inherits="PDF2016.upload" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8.     <title>uploadify3.2上传文件</title>  
  9.     <script src="js/jquery-1.10.2.min.js"></script>  
  10.     <link href="js/uploadify3.2/uploadify.css" rel="stylesheet" />  
  11.     <script src="js/uploadify3.2/jquery.uploadify.min.js"></script>  
  12.     <script type="text/javascript">  
  13.         $(function () {  
  14.             /*************setting***************/  
  15.             var definedData = [];  
  16.             definedData.auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";  
  17.             definedData.ASPSESSID = "@Session.SessionID";  
  18.             definedData.fileTypeExts = "*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.ppt;*.txt;*.rar;*.zip;*.exe";    //上传类型  
  19.             definedData.uploader = "/UploadHandler.ashx";    //后台处理路径  
  20.             definedData.fileSizeLimit = "2MB";  //上传大小  
  21.             definedData.fileObjName = "file_upload";    //控件名  
  22.             definedData.queueSizeLimit = 1;      //允许上传个数文件  
  23.             var data = { 'ASPSESSID': definedData.ASPSESSID, 'AUTHID': definedData.auth };    //firefox用swf上传丢失session  
  24.   
  25.             var errorData = [];  
  26.             errorData.err100 = "文件个数超出系统限制,只允许上传" + definedData.queueSizeLimit + "个文件!";  
  27.             errorData.err110 = "文件超出系统限制的大小,限制文件大小" + definedData.fileSizeLimit + "!";  
  28.             errorData.err120 = "文件大小异常!";  
  29.             errorData.err130 = "文件类型不正确,只允许上传后缀名" + definedData.fileTypeExts + "!";  
  30.             /*************setting***************/  
  31.             $("#file_upload").uploadify({  
  32.                 'buttonText''选择资源',  
  33.                 'swf''/js/uploadify3.2/uploadify.swf',  
  34.                 'uploader': definedData.uploader,  
  35.                 'auto'false//当文件被添加到队列时,自动上传  
  36.                 'formData': data, //上传时传递数据  
  37.                 'fileObjName': definedData.fileObjName,  
  38.                 'queueSizeLimit': definedData.queueSizeLimit,  
  39.                 'fileTypeExts': definedData.fileTypeExts,  
  40.                 'fileSizeLimit': definedData.fileSizeLimit,  
  41.                 'onUploadSuccess': function(file, data, response) {  
  42.                     $('#file_upload').uploadify('cancel''*'); //隐藏进度条</span>  
  43.                     var dataJson = JSON.parse(data);  
  44.                     if (dataJson.Status) {  
  45.                         //上传成功  
  46.                         alert(dataJson.Message);  
  47.                     } else {  
  48.                         //上传失败  
  49.                         alert(dataJson.Message);  
  50.                     }  
  51.                 },  
  52.                 //返回一个错误,选择文件的时候触发  
  53.                 'onSelectError': function (file, errorCode, errorMsg) {  
  54.                     switch (errorCode) {  
  55.                         case -100:  
  56.                             alert(errorData.err100);  
  57.                             break;  
  58.                         case -110:  
  59.                             alert(errorData.err110);  
  60.                             break;  
  61.                         case -120:  
  62.                             alert(errorData.err120);  
  63.                             break;  
  64.                         case -130:  
  65.                             alert(errorData.err130);  
  66.                             break;  
  67.                     }  
  68.                 },  
  69.                 //检测FLASH失败调用    
  70.                 'onFallback': function () {  
  71.                     alert("您未安装FLASH控件,无法上传!请安装FLASH控件后再试。");  
  72.                 }  
  73.             });  
  74.         });  
  75.     </script>  
  76. </head>  
  77. <body>  
  78.     <form id="form1" runat="server">  
  79.         <div>  
  80.             <p><input type="file" name="file_upload" id="file_upload" /></p>  
  81.             <p>  
  82.                 <img src="js/uploadify3.2/upload.jpg" style="cursor: pointer;" onclick="javascript:$('#file_upload').uploadify('upload')" />  
  83.             </p>  
  84.         </div>  
  85.     </form>  
  86. </body>  
  87. </html>  

[csharp] view plain copy
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.IO;  
  6. using Newtonsoft.Json;  
  7.   
  8. namespace PDF2016  
  9. {  
  10.     /// <summary>  
  11.     /// UploadHandler 的摘要说明  
  12.     /// </summary>  
  13.     public class UploadHandler : IHttpHandler  
  14.     {  
  15.   
  16.         public void ProcessRequest(HttpContext context)  
  17.         {  
  18.             context.Response.ContentType = "text/plain";  
  19.             context.Response.Charset = "utf-8";  
  20.   
  21.             UploadResult result = new UploadResult();  
  22.             HttpPostedFile file = context.Request.Files["file_upload"];  
  23.             string folder = context.Server.MapPath("~/upload/");  
  24.             if (file != null && file.ContentLength > 0)  
  25.             {  
  26.                 if (!Directory.Exists(folder))  
  27.                 {  
  28.                     Directory.CreateDirectory(folder);  
  29.                 }  
  30.                 file.SaveAs(Path.Combine(folder, file.FileName));  
  31.                 result.Status = true;  
  32.                 result.Message =  file.FileName;  
  33.             }  
  34.             else  
  35.             {  
  36.                 result.Status = false;  
  37.                 result.Message = "请选择上传文件";  
  38.             }  
  39.             string json = JsonConvert.SerializeObject(result, Formatting.Indented);  
  40.             context.Response.Write(json);  
  41.         }  
  42.   
  43.   
  44.         public class UploadResult  
  45.         {  
  46.             public bool Status;  
  47.             public string Message;  
  48.         }  
  49.   
  50.         public bool IsReusable  
  51.         {  
  52.             get { return false; }  
  53.         }  
  54.     }  
  55. }  
[csharp] view plain copy
 print?
  1. //在Global.asax中添加如下代码  
  2. //using System.Web.Security;  
  3. protected void Application_BeginRequest(object sender, EventArgs e)  
  4. {  
  5.     try  
  6.     {  
  7.         string session_param_name = "ASPSESSID";  
  8.         string session_cookie_name = "ASP.NET_SESSIONID";  
  9.   
  10.         if (HttpContext.Current.Request.Form[session_param_name] != null)  
  11.         {  
  12.             UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);  
  13.         }  
  14.         else if (HttpContext.Current.Request.QueryString[session_param_name] != null)  
  15.         {  
  16.             UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);  
  17.         }  
  18.     }  
  19.     catch (Exception)  
  20.     {  
  21.     }  
  22.   
  23.     try  
  24.     {  
  25.         string auth_param_name = "AUTHID";  
  26.         string auth_cookie_name = FormsAuthentication.FormsCookieName;  
  27.   
  28.         if (HttpContext.Current.Request.Form[auth_param_name] != null)  
  29.         {  
  30.             UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);  
  31.         }  
  32.         else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)  
  33.         {  
  34.             UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);  
  35.         }  
  36.   
  37.     }  
  38.     catch (Exception)  
  39.     {  
  40.     }  
  41.   
  42.   
  43. }  
  44. void UpdateCookie(string cookie_name, string cookie_value)  
  45. {  
  46.     HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);  
  47.     if (cookie == null)  
  48.     {  
  49.         HttpCookie cookie1 = new HttpCookie(cookie_name, cookie_value);  
  50.         Response.Cookies.Add(cookie1);  
  51.     }  
  52.     else  
  53.     {  
  54.         cookie.Value = cookie_value;  
  55.         HttpContext.Current.Request.Cookies.Set(cookie);  
  56.     }  
  57. }
原创粉丝点击