// 图片上传demo jquery(function () { var $ = jquery, $list = $('#pdflist'), // 优化retina, 在retina下这个值是2 ratio = window.devicepixelratio || 1, // 缩略图大小 thumbnailwidth = ratio, thumbnailheight = ratio, // web uploader实例 uploader; // 初始化web uploader uploader = webuploader.create({ // 自动上传。 auto: false, resize: false, // swf文件路径 swf: '../plugin/webuploader/js/uploader.swf', // 文件接收服务端。 server: '../plugin/webuploader/fileupload.php', // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: '#filepicker', filenumlimit: 1, filesizelimit: 100 * 1024 * 1024,//100m //chunked:true, compress: null, // 只允许选择文件,可选。 accept: { title: 'pdfs', extensions: 'pdf', mimetypes: 'pdf/*' }, formdata: { uptype: 'pdf' } }); uploader.on('error', function (msg) { if (msg == 'q_exceed_num_limit') { layer.msg('添加的文件数量超过了1个,只保留第一个'); } else if (msg == 'q_exceed_size_limit') { layer.msg('添加的文件总大小超过了100m'); } else if (msg == 'q_type_denied') { layer.msg('文件类型必须为“pdf”'); } }); $("#filepicker").on('click', function () { uploader.reset(); }) // 当有文件添加进来的时候 uploader.on('filequeued', function (file) { $("#startupload").text("开始上传"); $("#pdf_url").val(""); $list.children(".info").text(file.name); // 开始上传 $("#startupload").on('click', function () { uploader.upload(); }) }); $list.on("click", ".close", function () { var fileitem = $(this).parent(); var file_id = $(fileitem).attr("id"); if (file_id != undefined && file_id != '') { uploader.removefile(file_id, true); $(this).parents(".uploader-list").nextall("#img_url").attr("value", ""); $(this).parent(".file-item").remove(); } else { $(this).parents(".uploader-list").nextall("#img_url").attr("value", ""); $(this).parent(".file-item").remove(); } }); // 文件上传成功,给item添加成功class, 用样式标记上传成功。 uploader.on('uploadsuccess', function (file, response) { var url = response.url; // 将文件地址返回给页面提价至数据库 if (response.url != '') { $('#pdf_url').val(url); } $("#startupload").text("已上传"); }); // 文件上传失败,现实上传出错。 uploader.on('uploaderror', function (file) { var $li = $('#' + file.id), $error = $li.find('div.error'); // 避免重复创建 if (!$error.length) { $error = $('
').appendto($li); } $error.text('上传失败'); }); // 完成上传完了,成功或者失败,先删除进度条。 uploader.on('uploadcomplete', function (file) { $('#' + file.id).find('.progress').remove(); }); }); function bytestosize(bytes) { if (bytes === 0) return '0 b'; var k = 1000, // or 1024 sizes = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'], i = math.floor(math.log(bytes) / math.log(k)); return (bytes / math.pow(k, i)).toprecision(3) + ' ' + sizes[i]; }