Jquery插件 bootstrap fileinput 文件上傳工具


這是我上傳的第二個plugin 首先第一點就是因為這個好看 符合bootstrap的界面風格 第二是可以拖拽(雖然我不常用這個功能 但是這樣界面看起來就豐滿了很多) 最后不得不吐槽這個的回發事件 我百度了很久才找到 CSDN上面也問了 不知道是自己百度的方式不正確還是別的什么原因..好吧 我蠢

地址 

http://plugins.krajee.com/file-input

https://github.com/kartik-v/bootstrap-fileinput

效果圖

這里以我一個項目的新建產品為例 是基於MVC框架的 樣子是不是很好看

上傳之前

選中圖片的效果

上傳完成之后

如何使用

引入文件

css fileinput.cs

js fileinput.js、fileinput_locale_zh.js(漢化包)

代碼

html:

accept為需要控制的文件格式

1 <input id="imgUpload" type="file" class="file-loading" accept="image/*">

js:

language: 'zh'完成漢化 默認為英文,autoReplace允許替換 maxFileCount:1 這里說明一下 我做的是上傳單個的 如果需要批量上傳的 可以修改這兩個參數 allowedFileExtensions: ["jpg", "png", "gif"]就是驗證你上傳文件的格式 這里是圖片文件 previewFileIcon 是設置按鈕樣式 bootstrap提供了幾種按鈕顏色 以及大量的ICON 

.on("fileuploaded", function (e, data) {...} }) 這個就是我卡很久的地方了 先是不知道通過fileuploaded接收 然后是controller里的json不知道哪里取 這里是在data.response中有你return的json

$("#imgUpload")
        .fileinput({
        language: 'zh',
        uploadUrl: "/Product/imgDeal",
        autoReplace: true,
        maxFileCount: 1,
        allowedFileExtensions: ["jpg", "png", "gif"],
        browseClass: "btn btn-primary", //按鈕樣式 
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
        })
    .on("fileuploaded", function (e, data) {
        var res = data.response;
        if (res.state > 0) {
            alert('上傳成功');
            alert(res.path);
        }
        else {
            alert('上傳失敗')
        }
    })
View Code

Controller:

這里沒什么可說的 寫的都很明確了

[HttpPost]
        public ActionResult imgDeal()
        {
            uploadImages img = new uploadImages();
            var image = Request.Files;
            if (image != null && image.Count > 0)
            {
                string savePath = "../Uploads/";
                var _image = image[0];
                string _imageExt = System.IO.Path.GetExtension(_image.FileName).ToLower();
                string _imageName = DateTime.Now.ToString("yyyyMMddhhmmss") + _imageExt;
                //保存
                _image.SaveAs(Server.MapPath(savePath + _imageName));

                img.state = 1;
                img.name = _imageName;
                img.path = savePath + _imageName;
            }
            else
            {
                img.state = 0;
            }

            return Json(img);
        }
View Code

這樣就OK了

轉載請注明地址:http://www.cnblogs.com/CoffeeEddy/p/5167666.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM