123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- define(['jquery', 'bootstrap', 'backend', 'config', 'plupload'], function ($, undefined, Backend, Config, Plupload) {
- var Upload = {
- list: {},
- config: {
- container: document.body,
- classname: '.plupload',
- },
- api: {
-
- plupload: function (element, onAfterUpload) {
- element = typeof element == 'undefined' ? Upload.config.classname : element;
- $(element, Upload.config.container).each(function () {
- var id = $(this).prop("id");
- var url = $(this).data("url");
- var maxsize = $(this).data("maxsize");
- var mimetype = $(this).data("mimetype");
- var multipart = $(this).data("multipart");
- var multiple = $(this).data("multiple");
-
- url = url ? url : Config.upload.uploadurl;
-
- maxsize = maxsize ? maxsize : Config.upload.maxsize;
-
- mimetype = mimetype ? mimetype : Config.upload.mimetype;
-
- multipart = multipart ? multipart : Config.upload.multipart;
-
- multiple = multiple ? multiple : Config.upload.multiple;
-
- Upload.list[id] = new Plupload.Uploader({
- runtimes: 'html5,flash,silverlight,html4',
- multi_selection: multiple,
- browse_button: id,
- container: $(this).parent().get(0),
- flash_swf_url: '/assets/libs/plupload/js/Moxie.swf',
- silverlight_xap_url: '/assets/libs/plupload/js/Moxie.xap',
- filters: {
- max_file_size: maxsize,
- mime_types: mimetype
- },
- url: url,
- multipart_params: multipart,
- init: {
- PostInit: function () {
- },
- FilesAdded: function (up, files) {
- Plupload.each(files, function (file) {
-
-
- });
- $("#" + id).data("bakup-html", $("#" + id).html());
-
- setTimeout(function () {
- Upload.list[id].start();
- }, 1);
- },
- UploadProgress: function (up, file) {
-
-
- $("#" + id).prop("disabled", true).html("<i class='fa fa-upload'></i> 上传" + file.percent + "%");
- },
- FileUploaded: function (up, file, info) {
-
- $("#" + id).prop("disabled", false).html($("#" + id).data("bakup-html"));
-
-
-
- try {
- var response = JSON.parse(info.response);
- if (response.hasOwnProperty('code')) {
- response.code = response.code == 200 ? 1 : response.code;
- if (response.hasOwnProperty("url")) {
- response.data = response.url;
- }
- $("input[data-plupload-id='" + id + "-text']").val(response.data);
- var afterUpload = $("#" + id).data("after-upload");
- if (afterUpload && typeof Upload.api.custom[afterUpload] == 'function') {
- Upload.api.custom[afterUpload].call(info, id, response);
- }
- if (typeof onAfterUpload == 'function') {
- onAfterUpload.call(info, id, response);
- }
- } else {
- Toastr.error(e.message + "(code:-2)");
- }
- } catch (e) {
- Toastr.error(e.message + "(code:-1)");
- }
- },
- Error: function (up, err) {
- Toastr.error(err.message + "(code:" + err.code + ")");
- }
- }
- });
- Upload.list[id].init();
- });
- },
-
- send: function (file, callback) {
- var data = new FormData();
- data.append("file", file);
- $.each(Config.upload.multipart, function (k, v) {
- data.append(k, v);
- });
- $.ajax({
- url: Config.upload.uploadurl,
- data: data,
- cache: false,
- contentType: false,
- processData: false,
- type: 'POST',
- dataType: 'json',
- success: function (data) {
- if (data.hasOwnProperty("code")) {
- data.code = data.code == 200 ? 1 : data.code;
- if (data.hasOwnProperty("url")) {
- data.content = data.url;
- }
- var content = data.hasOwnProperty("content") && data.content != "" ? data.content : "";
- if (data.code === 1) {
-
- if (typeof callback == 'function') {
- callback.call(this, data);
- } else {
- Toastr.success(content ? content : __('Operation completed'));
- }
- } else {
- Toastr.error(content ? content : __('Operation failed'));
- }
- } else {
- Toastr.error(__('Unknown data format'));
- }
- }, error: function () {
- Toastr.error(__('Network error'));
- }
- });
- },
- custom: {
-
- afteruploadcallback: function (id, response) {
- console.log(this, id, response);
- alert("Custom Callback,Response URL:" + response.url);
- },
- },
- }
- };
- return Upload;
- });
|