1. 程式人生 > 實用技巧 >html 多圖片上傳 預覽 ajax 提交 附件上傳 js獲取uuid

html 多圖片上傳 預覽 ajax 提交 附件上傳 js獲取uuid

樣式

a{
    text-decoration: none;
}
.div_enclosure{
    padding: 5px;
}
.div_enclosure p{
    font-size: 13px;
    line-height: 15px !important;
    margin-bottom: 5px;
}
.enclosures .div_img_preview {
    width: 60px;
    height: 60px;
    margin-top: 10px;
    margin-right: 10px;
    float: left;
    position
: relative; } .enclosures .div_img_preview img{ width: 100%; height: 100%; } /*樣式1*/ .a-upload { background-image: url('/content/img/ic_add_image.png'); background-size: cover; cursor: pointer; overflow: hidden; display: inline-block; *display: inline; *zoom
: 1 } .a-upload input { position: absolute; font-size: 100px; right: 0; top: 0; opacity: 0; filter: alpha(opacity=0); cursor: pointer } .span_close{ display: block; width: 20px; height: 20px; background-image: url('/content/img/ic_delete_menu.png')
; background-size: cover; position: absolute; top: -10px; right: -10px; }
style.css

js

//生成uuid()
function getUuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
  
    var uuid = s.join("");
    return uuid;
}
獲取uuid
function AddUpload(divId) {
    var div = $('<div class="div_img_preview a-upload"></div>')
    var inputFile = $('<input type="file" name="files" id="file1" accept="image/*" value="上傳" multiple />');
    $(inputFile).on("change", function (e) {
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;

        //預覽
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];

            var filetype = file.type;
            var size = file.size;
            if (size > 10 * 1024 * 1024) {
                alert("請上傳小於10M的圖片");
            }
            else {
                if (url) {
                    src = url.createObjectURL(file);
                } else {
                    src = e.target.result;
                }

                var uuid = getUuid();
                curFiles.push(
                    {
                        uuid: uuid,
                        file: file
                    });

                var previewDiv = $('<div class="div_img_preview" uuid="' + uuid + '"><img src="' + src + '" /></div>');
                var closeBtn = $('<span class="span_close"></span>');
                $(closeBtn).click(function () {
                    var thisdiv = $(this).parent();
                    var thisuuid = $(thisdiv).attr("uuid");
                    curFiles.removeKey("uuid", thisuuid);
                    $(thisdiv).remove();
                });
                $(previewDiv).append(closeBtn);
                $(this).parent().before(previewDiv);
            }
        }
        if (files.length > 0) {
            //隱藏當前上傳按鈕
            $(this).parent().hide();
            //新增新上傳按鈕
            AddUpload(divId);
        }
    });
    $(div).append(inputFile);
    $("#" + divId).append(div);
}
index.js

呼叫

<div class="div_enclosure">
                <p>附件:</p>
                <div class="enclosures" id="divEnclosures">

                </div>
            </div>

<script>
var curFiles = [];//用於儲存上傳後的圖片

        //新增上傳按鈕
        AddUpload("divEnclosures");



 function submit() {
        var form = new FormData($("#createForm")[0]);
        for (var [a, b] of form.entries()) {
            //如果value是file型別的
            if (b instanceof File) {
                if (b.name != "") {
                    //先將表單中原有的fileList物件中的檔案刪除
                    form.delete(a);
                    for (var i = 0; i < curFiles.length; i++) {
                        //然後再將curFiles的檔案追加到表單中
                        form.append(a, curFiles[i].file);
                    }
                    //var f1 = new File([],"");
                    //form.append(a,f1);
                }
            }
        }
        $.ajax({
            url: BaseUrl + 'pm/message/saveMeaasge',
            type: 'post',
            cache: false,
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                location.href = returl;
            }
        });
    }
</script>
demo.html

附件

注:以上程式碼屬個人整理,用於交流學習,原創。轉載請標明出處。(QQ/微信:742010299 暱稱:同心同德)