123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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 that = this;
- 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;
- url = Backend.api.fixurl(url);
-
- 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) {
-
-
- });
- $(that).data("bakup-html", $(that).html());
-
- setTimeout(function () {
- Upload.list[id].start();
- }, 1);
- },
- UploadProgress: function (up, file) {
-
-
- $(that).prop("disabled", true).html("<i class='fa fa-upload'></i> 上传" + file.percent + "%");
- },
- FileUploaded: function (up, file, info) {
-
- $(that).prop("disabled", false).html($(that).data("bakup-html"));
-
-
-
- try {
- var ret = JSON.parse(info.response);
- if (ret.hasOwnProperty('code')) {
- ret.data = ret.code == 200 ? ret : ret.data;
- ret.code = ret.code == 200 ? 1 : ret.code;
- var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : null;
- var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
- if (ret.code === 1) {
-
- if ($(that).data("input-id")) {
- $("input#" + $(that).data("input-id")).val(data.url);
- }
- var afterUpload = $("#" + id).data("after-upload");
- if (afterUpload && typeof Upload.api.custom[afterUpload] == 'function') {
- Upload.api.custom[afterUpload].call(that, data);
- }
- if (typeof onAfterUpload == 'function') {
- onAfterUpload.call(that, data);
- }
- } else {
- Toastr.error(msg ? msg : __('Operation failed'));
- }
- } 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 (ret) {
- if (ret.hasOwnProperty("code")) {
- ret.data = ret.code == 200 ? ret : ret.data;
- ret.code = ret.code == 200 ? 1 : ret.code;
- var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : null;
- var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
- if (ret.code === 1) {
-
- if (typeof callback == 'function') {
- callback.call(this, data);
- } else {
- Toastr.success(msg ? msg : __('Operation completed'));
- }
- } else {
- Toastr.error(msg ? msg : __('Operation failed'));
- }
- } else {
- Toastr.error(__('Unknown data format'));
- }
- }, error: function () {
- Toastr.error(__('Network error'));
- }
- });
- },
- custom: {
-
- afteruploadcallback: function (response) {
- console.log(this, response);
- alert("Custom Callback,Response URL:" + response.url);
- },
- },
- }
- };
- return Upload;
- });
|