百度文件上传 WebUploader 常用方法和多个上传

 JavaScript  2021-05-25  admin  1141  1540

百度文件上传 WebUploader 常用方法和多个上传

<div class="comment-upload" data-id="1">
    <div class="upload-pics">
        <div class="up-btn up-btn-1">
            <img src="/images/upload.png">
            <p>添加图片/视频</p>
        </div>
    </div>
</div>
<div class="comment-upload" data-id="2">
    <div class="upload-pics">
        <div class="up-btn up-btn-2">
            <img src="/images/upload.png">
            <p>添加图片/视频</p>
        </div>
    </div>
</div>
<div class="comment-upload" data-id="3">
    <div class="upload-pics">
        <div class="up-btn up-btn-3">
            <img src="/images/upload.png">
            <p>添加图片/视频</p>
        </div>
    </div>
</div>
var ary_comments_upload_file = {
    "comments_allow_picture_ext": ["jpg", "png", "gif", "jpeg"],
    "comments_allow_video_ext": ["mp4", "avi", "mov", "rmvb", "rm", "flv", "3GP"],
    "comments_allow_picture_max_size": 10,
    "comments_allow_video_max_size": 50
};
$(".comment-upload").each(function () {
    var _index = $(this).attr('data-id');
    var file_size = 0;

    //文件上传
    uploader[_index] = WebUploader.create({
        swf: "/upload/webuploader/Uploader.swf",
        server: '/wap/comment/upload',
        pick: $(".up-btn-" + _index),
        accept: {
            title: '文件上传',
            extensions: 'mp4,mov,rmvb,rm,mkv,wmv,avi,jpeg,png,gif,jpg',      // 可以多个后缀,以逗号分隔, 不要有空格
            mimeTypes: 'video/*,image/*'
        },
        chunked: true,//开启分片上传
        chunkSize: 5 * 1024 * 1024,//每片文件大小上限
        threads: 1,//上传并发数
        //不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
        resize: false,
        fileSizeLimit: 100 * 1024 * 1024, //可传文件大小上限
        //fileSingleSizeLimit: 100 * 1024 * 1024, //单片文件大小上限
        auto: true,//有文件就自动上传
        duplicate: true,//文件去重
        //fileNumLimit: 7,
        formData: {

        },
        compress: {compressSize: 100 * 1024 * 1024}
    });

    // 当文件上传成功时触发。
    uploader[_index].on('uploadSuccess', function (file, res) {
        if (res['status'] == false) {
            layer.msg(res.msg);
            return;
        }
    });

    // 当某个文件的分块在发送前触发,主要用来询问是否要添加附带参数,大文件在开起分片上传的前提下此事件可能会触发多次。
    uploader[_index].on('uploadBeforeSend', function (object, data, headers) {
        data.file_size = object.total;
    });

    // 当文件被加入队列之前触发,此事件的handler返回值为false,则此文件不会被添加进入队列。
    uploader[_index].on('beforeFileQueued', function (file) {
        var type = file.type;
        type = type.substring(0, 5);
        file_size = file.size / 1024 / 1024;
        var file_ext = file.ext;
        if (!in_array(ary_comments_upload_file['comments_allow_picture_ext'], file_ext) && !in_array(ary_comments_upload_file['comments_allow_video_ext'], file_ext)) {
            layer.msg('不支持的文件格式!');
        }

        if (type == 'image') {
            if (file_size > ary_comments_upload_file['comments_allow_picture_max_size']) {
                layer.msg('只允许上传 ' + ary_comments_upload_file['comments_allow_picture_max_size'] + 'MB 以内的图片!');
                return false;
            }

        } else if (type == 'video') {
            if (file_size > ary_comments_upload_file['comments_allow_video_max_size']) {
                layer.msg('只允许上传 ' + ary_comments_upload_file['comments_allow_video_max_size'] + 'MB 以内的视频!');
                return false;
            }
        }
    });

    // 某个文件开始上传前触发,一个文件只会触发一次。
    uploader[_index].on('uploadStart', function () {
        layer_index = layer.load(2);
    });

    // 不管成功或者失败,文件上传完成时触发。
    uploader[_index].on('uploadComplete', function (file) {
        int_image = 0;
        int_video = 0;
        layer.close(layer_index);
    });

    // 当一批文件添加进队列以后触发。
    uploader[_index].on('filesQueued', function (files) {
        for (var i = 0, len = files.length; i < len; i++) {
            var type = files[i]['type'];
            type = type.substring(0, 5);
            if (type == 'image') {
                console.log(type);
            } else if (type == 'video') {
                console.log(type);
            }
        }
    });

    // 当validate不通过时,会以派送错误事件的形式通知调用者。
    uploader[_index].on('error', function (type) {
        console.log(type);
    });

});

function in_array(array, val) {
    for (var i = 0; i < array.length; i++) {
        if (val == array[i]) {
            return true;
        }
    }
    return false;
}


如果文章对您有帮助,点击下方的广告,支持一下作者吧!

相关推荐


百度 WebUploader 文件大小和发送接收和实际大小不一致

百度 WebUploader 文件大小和发送接收和实际大小不一致

Saber, 基于swoole 的PHP异步协程HTTP客户端

HTTP军刀(呆毛王),Swoole人性化组件库之PHP高性能HTTP客户端, 基于Swoole原生协程, 支持多种风格操作, 底层提供高性能解决方案, 让开发者专注于功能开发, 从传统同步阻塞且配置繁琐的Curl中解放.基于Swoole协程Client开发人性化使用风格, ajax.js/axios.js/requests.py用户福音, 同时支持PSR风格操作浏览器级别完备的Cookie管理机

php 多文件上传

php 多文件上传html&lt;formmethod=&quot;post&quot;action=&quot;upload_multiple.php&quot;enctype=&quot;multipart/form-data&quot;&gt; &lt;inputtype=&#39;file&#39;name=&quot;upload[]&quot;accept=&quot;image/*

html 文件上传控件 input=file 美化

html 文件上传控件 input=file 美化&lt;styletype=&quot;text/css&quot;&gt; form{ width:50px; margin:100pxauto; } .fileDiv{ width:100px; height:40px; background-color:#00a2d4; line-height:40px; text