jquery外掛uploadify實作帶有進度條的檔案批次上傳_jquery
有時專案中需要一個檔案批次上傳功能時,個人認為uploadify是快速簡單的解決方案,分享給大家供大家參考,具體如下
先上效果圖:
具體程式碼如下:
在頁中如下
完整頁面代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>文件批量上传Demo</title> <!--引入Jquery--> <script src="js/jquery-1.11.3.min.js"></script> <!--引入uploadify--> <script type="text/javascript" src="uploadify/jquery.uploadify.js"></script> <link type="text/css" href="uploadify/uploadify.css" rel="stylesheet" /> <script type="text/javascript"> $(function () { var guid = '<%=Request["guid"] %>'; var type = '<%=Request["type"] %>'; if (guid == null || guid == "") { guid = newGuid(); } if (type != null) { type = type + '/'; } $('#file_upload').uploadify({ 'swf': 'uploadify/uploadify.swf', //FLash文件路径 'buttonText': '浏 览', //按钮文本 'uploader': 'uploadhandler.ashx?guid=' + guid, //处理ASHX页面 'formData': { 'folder': 'picture', 'isCover': 1 }, //传参数 'queueID': 'fileQueue', //队列的ID 'queueSizeLimit': 10, //队列最多可上传文件数量,默认为999 'auto': false, //选择文件后是否自动上传,默认为true 'multi': true, //是否为多选,默认为true 'removeCompleted': true, //是否完成后移除序列,默认为true 'fileSizeLimit': '0', //单个文件大小,0为无限制,可接受KB,MB,GB等单位的字符串值 'fileTypeDesc': 'All Files', //文件描述 'fileTypeExts': '*.*', //上传的文件后缀过滤器 'onQueueComplete': function (queueData) { //所有队列完成后事件 alert("上传完毕!"); }, 'onError': function (event, queueId, fileObj, errorObj) { alert(errorObj.type + ":" + errorObj.info); }, 'onUploadStart': function (file) { }, 'onUploadSuccess': function (file, data, response) { //一个文件上传成功后的响应事件处理 //var data = $.parseJSON(data);//如果data是json格式 //var errMsg = ""; } }); }); function newGuid() { var guid = ""; for (var i = 1; i <= 32; i++) { var n = Math.floor(Math.random() * 16.0).toString(16); guid += n; if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) guid += "-"; } return guid; } //执行上传 function doUpload() { $('#file_upload').uploadify('upload', '*'); } </script> </head> <body> <form id="form1" runat="server" enctype="multipart/form-data"> <div id="fileQueue" class="fileQueue"></div> <div> <input type="file" name="file_upload" id="file_upload" /> <p> <input type="button" class="shortbutton" id="btnUpload" onclick="doUpload()" value="上传" /> <input type="button" class="shortbutton" id="btnCancelUpload" onclick="$('#file_upload').uploadify('cancel')" value="取消" /> </p> <div id="div_show_files"></div> </div> </form> </body> </html>
UploadHandler.ashx程式碼:
using System; using System.Web; using System.IO; public class UploadHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Request.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); context.Response.Charset = "UTF-8"; if (context.Request.Files.Count > 0) { #region 获取上传路径 string uploadFolder = GetUploadFolder(); #endregion if (System.IO.Directory.Exists(uploadFolder)) {//如果上传路径存在 HttpPostedFile file = context.Request.Files["Filedata"]; string filePath = Path.Combine(uploadFolder, file.FileName); file.SaveAs(filePath); context.Response.Write("0"); } else { context.Response.Write("2"); } } } public bool IsReusable { get { return false; } } /// <summary> /// 返回不带后缀的文件名 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string GetFirstFileName(string fileName) { return Path.GetFileNameWithoutExtension(fileName); } /// <summary> /// 获取上传目录 /// </summary> /// <returns></returns> public static string GetUploadFolder() { string rootPath = HttpContext.Current.Server.MapPath("~"); return Path.Combine(rootPath, "test"); } }
檔案上傳.NET預設有大小限制,像IIS限制的30M預設請求大小。如果不想修改IIS,又想突破這個大小的限制,例如上傳1GB大小的檔案。
這是修改Web.config即可實現。
<?xml version="1.0" encoding="utf-8"?> <!-- --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime maxRequestLength="1073741824"/> </system.web> <!--用于设置文件上传的最大允许大小(单位:bytes)--> <system.webServer> <security> <requestFiltering> <!--修改服务器允许最大长度(1GB)--> <requestLimits maxAllowedContentLength="1073741824"/> </requestFiltering> </security> </system.webServer> </configuration>
希望本文所述對大家學習javascript程式設計有所幫助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。
