JQuery fileupload插件實(shí)現(xiàn)文件上傳功能
道理相通,我簡(jiǎn)單分享下在.net MVC下的實(shí)裝。
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.實(shí)裝controller里的對(duì)應(yīng)方法,我這個(gè)處理邏輯比較復(fù)雜,懶得修改了,反正就這個(gè)意思
//----------------------------------------------------------------
/// <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.頁面的實(shí)裝
@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>
}
√,就是這個(gè)樣子
還附贈(zèng)了一個(gè)簡(jiǎn)易loding的實(shí)現(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)容,請(qǐng)點(diǎn)擊《jQuery上傳操作匯總》,進(jìn)行深入學(xué)習(xí)和研究。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(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實(shí)現(xiàn)上傳文件
- jquery獲取file表單選擇文件的路徑、名字、大小、類型
相關(guān)文章
jQuery)擴(kuò)展jQuery系列之一 模擬alert,confirm(一)
很多人都為了使alert系統(tǒng)的調(diào)用函數(shù)在自己的控制范圍之內(nèi),都選擇了去封裝一個(gè)屬于自己的alert組件,現(xiàn)在我們就動(dòng)手實(shí)現(xiàn)一個(gè)這樣的小部件。2010-12-12
jQuery結(jié)合HTML5制作的愛心樹表白動(dòng)畫
這篇文章主要給大家分享的是一則jQuery結(jié)合HTML5制作的愛心樹表白動(dòng)畫的特效代碼,各位宅在家里的程序猿們,再辛苦的工作,也不要忘記你們的女朋友哦。2015-02-02
jQuery 動(dòng)態(tài)云標(biāo)簽插件
這里推薦給大家一款jquery動(dòng)態(tài)云標(biāo)簽的插件,非常的炫,在指定塊級(jí)元素內(nèi)動(dòng)態(tài)生成a標(biāo)簽,a標(biāo)簽的高度、寬度、位置、層數(shù)、背景顏色隨機(jī)可控,a標(biāo)簽漸隱顯示和漸隱消失,可改變初始化的透明度,鼠標(biāo)懸浮停止動(dòng)畫且透明度最大,層數(shù)最高,鼠標(biāo)離開,恢復(fù)之前狀態(tài)2014-11-11
jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法
這篇文章主要介紹了jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法,涉及jQuery響應(yīng)鍵盤事件及ajax調(diào)用php文件針對(duì)輸入內(nèi)容的處理與回調(diào)相關(guān)技巧,非常簡(jiǎn)單易懂,需要的朋友可以參考下2016-05-05
jquery validate 實(shí)現(xiàn)動(dòng)態(tài)增加/刪除驗(yàn)證規(guī)則操作示例
這篇文章主要介紹了jquery validate 實(shí)現(xiàn)動(dòng)態(tài)增加/刪除驗(yàn)證規(guī)則操作,結(jié)合實(shí)例形式分析了jQuery validate表單驗(yàn)證相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
jQuery實(shí)現(xiàn)簡(jiǎn)單的手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單的手風(fēng)琴效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
使用jQuery實(shí)現(xiàn)驗(yàn)證上傳圖片的格式與大小
在項(xiàng)目中,我們經(jīng)常要遇到上傳圖片,這就需要我們必須要驗(yàn)證圖片的格式與大小,那么如何來操作呢,今天就給大家分享一個(gè)非常簡(jiǎn)單的jQuery驗(yàn)證上傳圖片的格式與大小的代碼。2014-12-12

