引自 文章 http://www.cnblogs.com/babycool/archive/2012/08/04/2623137.htmljavascript
將文章裏的代碼整合在了一個解決方案裏,直接能夠下載測試,上代碼先css
前臺html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title></title> <link href="js/uploadify/uploadify.css" rel="stylesheet" type="text/css" /> <script src="js/uploadify/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src="js/uploadify/jquery.uploadify-3.1.js" type="text/javascript"></script> <script src="js/uploadify/uploadHandle2_ashx.js" type="text/javascript"></script> </head> <body> <div> <%--用來做爲文件隊列區域--%> <div id="fileQueue"> </div> <input type="file" name="uploadify" id="uploadify" /> <div> <a href="javascript:$('#uploadify').uploadify('upload')">上傳</a>| <a href="javascript:$('#uploadify').uploadify('cancel')">取消上傳</a> </div> </div> </body> </html>
後臺ashx文件java
<%@ WebHandler Language="C#" Class="imageHandler" %> using System; using System.Web; using System.IO; public class imageHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //接收上傳後的文件 HttpPostedFile file = context.Request.Files["Filedata"]; //其餘參數 //string somekey = context.Request["someKey"]; //string other = context.Request["someOtherKey"]; //獲取文件的保存路徑 string uploadPath = HttpContext.Current.Server.MapPath("UploadImages" + "\\"); //判斷上傳的文件是否爲空 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; } } }
示例解決方案下載 ajax無刷新上傳圖片_2013_12_19_14_17.rarjquery