JQuery fileupload插件實現(xiàn)文件上傳功能
道理相通,我簡單分享下在.net MVC下的實裝。
1.制作Model類
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace RCRS.WebApp.LG.EM.Models { //---------------------------------------------------------------- /// <summary> /// Import畫面用 /// </summary> //---------------------------------------------------------------- public class tmp_UploadFile { /// <summary></summary> public HttpPostedFileBase FileName { get; set; } } }
2.實裝controller里的對應(yīng)方法,我這個處理邏輯比較復(fù)雜,懶得修改了,反正就這個意思
//---------------------------------------------------------------- /// <summary> /// アップロード /// </summary> /// <returns></returns> //---------------------------------------------------------------- [HttpPost] public virtual ActionResult UploadFile() { HttpPostedFileBase uploadedFile = Request.Files["FileName"]; string message = "アップロード失敗しました。"; bool isUploaded = false; string path = ""; string dateTimeNow = DateTime.Now.ToString("yyMMdd-hhmmss"); string userName = User.Identity.GetUserName(); string uploadMsg = string.Empty; if (uploadedFile != null && uploadedFile.ContentLength != 0) { string pathForSaving = Server.MapPath("~/App_Data/Uploaded/"); try { if (BsnssBihin.IsExcel(uploadedFile.FileName)) { path = System.IO.Path.Combine(pathForSaving, dateTimeNow + "_" + uploadedFile.FileName); uploadedFile.SaveAs(path); isUploaded = BsnssBihin.UploadBihinChange(path, userName, ref uploadMsg); if (isUploaded) { message = "アップロード成功しました!" + "\n" + uploadMsg; Logger.Info("[成功]備品アップロード, " + dateTimeNow + ", " + "[" + userName + "]" + "[" + path + "]" + uploadMsg); } else { message = "アップロード失敗しました。" + "\n" + uploadMsg; Logger.Info("[失敗]備品アップロード, " + dateTimeNow + ", " + "[" + userName + "]" + "["+path + "]" + uploadMsg); } } else { message = "ファイルの形式は不正です。"; } } catch (Exception ex) { message = string.Format("失敗しました: {0}", ex.Message); Logger.Info("[失敗]備品アップロード: " + ex.Message + dateTimeNow + ", " + "[" + userName + "]" + "[" + path + "]"); } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); }
3.頁面的實裝
@model RCRS.WebApp.LG.EM.Models.tmp_UploadFile <table align="center" style="margin-bottom:200px"> <tr> <td> <div style="width:470px"> <input type="text" id="tbx-file-path" value="ファイルを選択してください" readonly="readonly" /> </div> </td> <td> <div style="width: 60px"> <span class="btn btn-primary fileinput-button"> <span>選 択</span> @Html.TextBoxFor(m => m.FileName, new { id = "file-upload", type = "file", accept = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" }) </span> </div> </td> <td> <div style="width:60px"> <a class="btn btn-primary" href="#" id="hl-start-upload">アップロード</a> </div> </td> </tr> </table> <div id="loadingOver" class="loadingOver"></div> <div id="dvloader" class="dvloader"> <span class="label label-info" style="align-content:center"> 処理中、少々お待ちください</span><br /> <br /> <img id="loadingGif" src="../Content/img/loader.gif" alt="" /> </div> @section scripts{ @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/common") @Scripts.Render("~/bundles/fileupload") <script type="text/javascript"> var data_upload; $(document).ready(function () { 'use strict'; $('#file-upload').fileupload({ url: '../Bihin/UploadFile', dataType: 'json', add: function (e, data) { data_upload = data; }, done: function (event, data) { if (data.result.isUploaded) { $("#tbx-file-path").val("ファイルを選択してください"); data_upload = ""; } $("#dvloader").css("display", "none"); $("#loadingOver").css("display", "none"); alert(data.result.message); }, fail: function (event, data) { data_upload = ""; if (data.files[0].error) { $("#dvloader").css("display", "none"); $("#loadingOver").css("display", "none"); alert(data.files[0].error); } } }); }); $("#hl-start-upload").on('click', function () { if (data_upload) { $("#dvloader").css("display", "block"); $("#loadingOver").css("display", "block"); data_upload.submit(); } return false; }); $("#file-upload").on('change', function () { $("#tbx-file-path").val(this.files[0].name); }); </script> }
√,就是這個樣子
還附贈了一個簡易loding的實現(xiàn)
貼出CSS代碼:
.dvloader { display:none; position:absolute; top:40%; left:40%; width:20%; height:20%; z-index:1001; text-align:center; font-size:1.5em; } .loadingOver { display:none; position:absolute; top:0; left:0; width:100%; height:100%; background-color:#f5f5f5; opacity:0.5; z-index:1000; }
這里,多說一嘴:
關(guān)于input 的accept屬性,這里只想讀入Excel,所以
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel
更多精彩內(nèi)容,請點擊《jQuery上傳操作匯總》,進(jìn)行深入學(xué)習(xí)和研究。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- jquery ajaxfileuplod 上傳文件 essyui laoding 效果【防止重復(fù)上傳文件】
- jquery-file-upload 文件上傳帶進(jìn)度條效果
- jQuery插件ajaxFileUpload使用詳解
- jQuery File Upload文件上傳插件使用詳解
- jQuery插件ajaxFileUpload異步上傳文件
- jQuery獲取file控件中圖片的寬高與大小
- Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件
- 從重置input file標(biāo)簽中看jQuery的 .val() 和 .attr(“value”) 區(qū)別
- jQuery插件ajaxfileupload.js實現(xiàn)上傳文件
- jquery獲取file表單選擇文件的路徑、名字、大小、類型
相關(guān)文章
jQuery)擴(kuò)展jQuery系列之一 模擬alert,confirm(一)
很多人都為了使alert系統(tǒng)的調(diào)用函數(shù)在自己的控制范圍之內(nèi),都選擇了去封裝一個屬于自己的alert組件,現(xiàn)在我們就動手實現(xiàn)一個這樣的小部件。2010-12-12jQuery+php實時獲取及響應(yīng)文本框輸入內(nèi)容的方法
這篇文章主要介紹了jQuery+php實時獲取及響應(yīng)文本框輸入內(nèi)容的方法,涉及jQuery響應(yīng)鍵盤事件及ajax調(diào)用php文件針對輸入內(nèi)容的處理與回調(diào)相關(guān)技巧,非常簡單易懂,需要的朋友可以參考下2016-05-05jquery validate 實現(xiàn)動態(tài)增加/刪除驗證規(guī)則操作示例
這篇文章主要介紹了jquery validate 實現(xiàn)動態(tài)增加/刪除驗證規(guī)則操作,結(jié)合實例形式分析了jQuery validate表單驗證相關(guān)操作技巧,需要的朋友可以參考下2019-10-10