1. 程式人生 > >webupload多檔案的上傳

webupload多檔案的上傳

前端頁面:

<li class="mui-table-view-cell">
    <p>營業執照</p>

    <div class="liBox">
        <div class="img" style="width: 100%">
            <div class="photo">
                <div class="photoInner">
                    <img src="__STATIC__/api/image/uploadPhoto/takePhoto.png" 
alt="" class="tianjiaImg"/> </div> </div> <img src="__STATIC__/api/image/editImg.png" class="editImg"/> <div class="upload-img " data-up="img1" data-aa=1></div> </div> </div> </li
>

javascript程式碼:


 
<script>
    var  aa=6;
    var normal_class = '';
    $('.img').on('click', function () {
        normal_class = $(this).find('.upload-img').attr('data-up');
        aa = $(this).find('.upload-img').attr('data-aa');

    });

    //圖片上傳
    $(function () {

        var 
pending; //最大上傳的照片數 var maxUpload = 5; var uploadCount = 0; var uploader = WebUploader.create({ pick: '.img', accept: { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', mimeTypes: 'image/*' }, thumb: false, compress: { width: 1000, height: 1000, quality: 90, allowMagnify: false, crop: false, preserveHeaders: true, noCompressIfLarger: false, compressSize: 0 }, auto: true, //formData: null, fileVal: 'file', duplicate: false, server: '{:url("upload_cmp_img")}', }); uploader.on('beforeFileQueued', function() { /* if(uploadCount >= maxUpload) { alert("最多上傳3張"); return false; }*/ uploader.option('formData',{ num:aa, }) uploader.upload(); }); uploader.on('uploadProgress', function (file, percentage) { }); uploader.on('uploadError', function (file, error) { alert('上傳失敗:' + error); }); uploader.on('error', function (type) { var message = '上傳失敗:' + type; switch (type) { case 'Q_EXCEED_NUM_LIMIT': message = '檔案數量超過系統限制'; break; case 'Q_EXCEED_SIZE_LIMIT': message = '檔案大小超過系統限制'; break; case 'Q_TYPE_DENIED': message = '系統不允許該檔案型別'; break; } alert(message); }); uploader.on('uploadComplete', function () { uploader.reset(); }); uploader.on('uploadSuccess', function (file, response) { // console.log(response); // console.log(file); if (response._raw) { console.log(response); uploadCount++; var raw = response._raw; var path = raw.substring(0,raw.indexOf("<")); if (normal_class !== '') { $('[data-up=' + normal_class + ']').html( '<div class="upload">' + '<img src="__STATIC__/api/image/editImg.png" ' + 'style=""' + ' class="delete-pics"/><img src="' + path + '"> ' + ' <input class="you" type="hidden" value="' + path + '" name="pics[]"/> ' + '</div>') } } else { alert('上傳失敗,請重試!'); } }); $(document).on('click', 'img.delete-pics', function () { uploadCount--; $(this).closest('.upload').remove(); }); }); </script>