: 使用uploadiify实现图片上传功能

来源:互联网 发布:网络推广电话销售话术 编辑:程序博客网 时间:2024/05/21 19:45
使用uploadify插件实现图片上传功能。这是一种flash上传的形式。缺点是在ff不能正常的显示。有说与session的问题,没解决成功。 

引入 jquery.uploadify.min.js插件和uploadify文件夹。可在官方文档中下载 
建一个upload.aspx 
Html代码 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="text" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>上传</title>  
  8.     <link type="text/css" rel="Stylesheet" href="Css/uploadify.css" />  
  9.     <script src="Js/jquery-1.7.2.min.js" type="text/javascript"></script>  
  10.     <script src="Js/Admin/jquery.uploadify-3.1.min.js" type="text/javascript"></script>  
  11.     <script type="text/javascript">  
  12.         $(document).ready(function() {  
  13.             var href = location.href;  
  14.             //alert(href);  
  15.             if (href.indexOf('?') < 0) { alert("错误"); return; }  
  16.             hrefhref = href.substring(href.indexOf('?') + 1, href.length);  
  17.             var foldername = href.substring(href.indexOf('=') + 1, href.length);  
  18.             var name=href.substring(0,href.indexOf('&'));  
  19.             //alert(href);  
  20.             $("#uploadify").uploadify({  
  21.                 height: 30,  
  22.                 swf: 'Images/AdministratorCenter/uploadify.swf',  
  23.                 uploader: 'UploadHandler.ashx?id=' + href,  
  24.                 width: 120,  
  25.                 buttonClass:'upload_button'  
  26.                 folder: 'WebImages',  
  27.                 fileTypeDesc: 'Web Images',  
  28.                 fileTypeExts: ' *.jpg;',  
  29.                 onUploadSuccess: function(file, data, response) { window.opener.document.all.uploadfile.innerHTML = foldername+"/"+name + ".jpg"; window.close(); },  
  30.                 onUploadError: function(file, errorCode, errorMsg, errorString) { alert(errorString); }  
  31.             });  
  32.         });    
  33.     </script>  
  34. </head>  
  35. <body>  
  36.     <form id="form1" runat="server">  
  37.     <input type="file" name="uploadify" id="uploadify" />  
  38.     </form>  
  39. </body>  
  40. </html>  


再建一个UploadHandler.ashx 
C#代码 
  1. <%@ WebHandler Language="C#" Class="UploadHandler" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5. using System.IO;  
  6.   
  7. public class UploadHandler : IHttpHandler {  
  8.   
  9.     public void ProcessRequest(HttpContext context)  
  10.     {  
  11.         context.Response.ContentType = "text/plain";  
  12.         context.Response.Charset = "utf-8";  
  13.   
  14.         HttpPostedFile file = context.Request.Files["Filedata"];  
  15.         string casetitle = context.Request["casetitle"];  
  16.         string uploadPath =  
  17.             HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\WebImages\\"+casetitle+"\\";  
  18.         string filenameSelt = context.Request["id"];  
  19.         if (file == null || filenameSelt == null)  
  20.         {  
  21.             return;  
  22.         }  
  23.         string filetype = file.FileName.Substring(file.FileName.IndexOf("."));//文件后缀  
  24.         if (file != null)  
  25.         {  
  26.             if (!Directory.Exists(uploadPath))  
  27.             {  
  28.                 Directory.CreateDirectory(uploadPath);  
  29.             }  
  30.             file.SaveAs(uploadPath+ filenameSelt + filetype);  
  31.               
  32.             //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失  
  33.             context.Response.Write("1");  
  34.         }  
  35.         else  
  36.         {  
  37.             context.Response.Write("0");  
  38.         }  
  39.     }  
  40.    
  41.     public bool IsReusable {  
  42.         get {  
  43.             return false;  
  44.         }  
  45.     }  
  46.   
  47. }  
原创粉丝点击