123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- var ZZXUpload = function(form, url, complete,noprocess) {
- if (url === null || url === '') {
- alert('url 不能为空!');
- return false;
- }
- var _self = this;
- var ZZXUpload, formData;
- _self.formData = $(form).length > 0 ? new FormData($(form)[0]) : new FormData();
- _self.url = url;
- _self.put = function(key, val) {
- _self.formData.append(key, val);
- }
- _self.upload = function(files) {
- uploadFiles(files);
- /*
- * _self.files = files; _self.files.length > 0 ?
- * uploadFile(_self.files.shift()) : '';
- */
- }
- var processFiles = function(files) {
- var file, _i, _len;
- for (_i = 0, _len = files.length; _i < _len; _i++) {
- file = files[_i];
- file.processing = true;
- file.status = Dropzone.UPLOADING;
- $(file.previewTemplate).addClass("dz-processing");
- }
- return uploadFiles(files);
- }
- var uploadFiles = function(files) {
- var file, formData, handleError, headerName, headerValue, headers, i, input, inputName, inputType, key, method, option, progressObj, response, updateProgress, url, value, xhr, _i, _j, _k, _l, _len, _len1, _len2, _len3, _m, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
- xhr = new XMLHttpRequest();
- for (_i = 0, _len = files.length; _i < _len; _i++) {
- file = files[_i];
- file.xhr = xhr;
- }
- xhr.open('post', _self.url, true);
- response = null;
- handleError = (function(_this) {
- return function() {
- var _j, _len1, _results;
- _results = [];
- for (_j = 0, _len1 = files.length; _j < _len1; _j++) {
- file = files[_j];
- }
- return _results;
- };
- })(this);
- updateProgress = (function(_this) {
- return function(e) {
- var allFilesFinished, progress, _j, _k, _l, _len1, _len2, _len3, _results;
- if (e != null) {
- progress = 100 * e.loaded / e.total;
- for (_j = 0, _len1 = files.length; _j < _len1; _j++) {
- file = files[_j];
- file.upload = {
- progress : progress,
- total : e.total,
- bytesSent : e.loaded
- };
- }
- } else {
- allFilesFinished = true;
- progress = 100;
- for (_k = 0, _len2 = files.length; _k < _len2; _k++) {
- file = files[_k];
- if (!(file.upload.progress === 100 && file.upload.bytesSent === file.upload.total)) {
- allFilesFinished = false;
- }
- file.upload.progress = progress;
- file.upload.bytesSent = file.upload.total;
- }
- if (allFilesFinished) {
- return;
- }
- }
- _results = [];
- for (_l = 0, _len3 = files.length; _l < _len3; _l++) {
- file = files[_l];
- _results.push(uploadprogress(file, progress,
- file.upload.bytesSent));
- }
- return _results;
- };
- })(this);
- xhr.onload = (function(_this) {
- return function(e) {
- var _ref;
- if(files.length > 0){
- if (files[0].status === Dropzone.CANCELED) {
- return;
- }
- }
- if (xhr.readyState !== 4) {
- return;
- }
- response = xhr.responseText;
- if (xhr.getResponseHeader("content-type")
- && ~xhr.getResponseHeader("content-type").indexOf(
- "application/json")) {
- try {
- response = JSON.parse(response);
- } catch (_error) {
- e = _error;
- response = "Invalid JSON response from server.";
- }
- }
- if(!noprocess){
- updateProgress();
- }
- typeof complete == 'function' ? complete(xhr.status,xhr.responseText) : '';
-
- if (!((200 <= (_ref = xhr.status) && _ref < 300))) {
- return handleError();
- } else {
- return true;
- }
- };
- })(this);
- xhr.onerror = (function(_this) {
- return function() {
- if (files[0].status === Dropzone.CANCELED) {
- return;
- }
- return handleError();
- };
- })(this);
- progressObj = (_ref = xhr.upload) != null ? _ref : xhr;
- progressObj.onprogress = updateProgress;
- headers = {
- "Accept" : "application/json",
- "Cache-Control" : "no-cache",
- "X-Requested-With" : "XMLHttpRequest"
- };
- for (headerName in headers) {
- headerValue = headers[headerName];
- xhr.setRequestHeader(headerName, headerValue);
- }
- formData = _self.formData;
- if(files.length > 0){
- for (i = _m = 0, _ref5 = files.length - 1; 0 <= _ref5 ? _m <= _ref5 : _m >= _ref5; i = 0 <= _ref5 ? ++_m : --_m) {
-
- var paramName = files[i].paramName == null ? files[i].name : files[i].paramName;
- paramName = paramName + '_' + i;
- formData.append(paramName, files[i]);
- }
- }
- return xhr.send(formData);
- };
- var uploadprogress = function(file, progress, bytesSent) {
- var node, _i, _len, _ref, _results;
- if (file.previewElement) {
- _ref = $(file.previewElement).find('[data-dz-uploadprogress]');
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- node = _ref[_i];
- if (node.nodeName === 'PROGRESS') {
- _results.push(node.value = progress);
- } else {
- _results.push(node.style.width = "" + progress + "%");
- }
- }
- return _results;
- }
- }
- var uploadFile = function(file) {
- var formData, handleError, input, inputElement, inputName, progressObj, xhr, _i, _len, _ref, _ref1, _this = this;
- xhr = new XMLHttpRequest();
- formData = new FormData();
- formData.append(file.name, file);
- xhr.open("POST", _self.url, true);
- handleError = function() {
- return _this.errorProcessing(file, xhr.responseText
- || ("Server responded with " + xhr.status + " code."));
- };
- xhr.onload = function(e) {
- var response;
- if (xhr.status !== 200) {
- return handleError();
- } else {
- var $preview = $(file.previewTemplate).find(
- ".progress-bar-success");
- $(file.previewTemplate).find(".progress-bar-success").css({
- width : "" + 100 + "%"
- });
- response = xhr.responseText;
- if (~xhr.getResponseHeader("content-type").indexOf(
- "application/json")) {
- response = $.parseJSON(response);
- }
- $(file.previewTemplate).addClass("success");
- previewTemplate: "<div class=\"preview file-preview\">\n <div class=\"details\">\n <div class=\"filename\"><span></span></div>\n </div>\n <div class=\"progress\"><span class=\"upload\"></span></div>\n <div class=\"success-mark\"><span>✔</span></div>\n <div class=\"error-mark\"><span>✘</span></div>\n <div class=\"error-message\"><span></span></div>\n</div>"
- _self.files.length > 0 ? uploadFile(_self.files.shift()) : '';
- return true;
- }
- };
- xhr.onerror = function() {
- return handleError();
- };
- progressObj = (_ref1 = xhr.upload) != null ? _ref1 : xhr;
- progressObj.onprogress = function(e) {
- var progress = Math.min(100, (e.loaded / e.total) * 100);
- var $preview = $(file.previewTemplate)
- .find(".progress-bar-success").css({
- width : "" + Math.max(0, progress + "%")
- });
- return true;
- };
- xhr.setRequestHeader("Accept", "application/json");
- xhr.setRequestHeader("Cache-Control", "no-cache");
- xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
- xhr.setRequestHeader("X-File-Name", file.name);
- return xhr.send(formData);
- };
- };
- /*document.write("<script src='assets/js/jquery.colorbox-min.js'></script>");
- $(function($) {
-
- var $overflow = '';
- var colorbox_params = {
- rel: 'colorbox',
- reposition:true,
- scalePhotos:true,
- scrolling:false,
- previous:'<i class="ace-icon fa fa-arrow-left"></i>',
- next:'<i class="ace-icon fa fa-arrow-right"></i>',
- close:'×',
- current:'{current} of {total}',
- maxWidth:'100%',
- maxHeight:'100%',
- onOpen:function(){
- $overflow = document.body.style.overflow;
- document.body.style.overflow = 'hidden';
- },
- onClosed:function(){
- document.body.style.overflow = $overflow;
- },
- onComplete:function() {
- $.colorbox.resize();
- }
- };
- $('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
- //let's add a custom loading icon
- $("#cboxLoadingGraphic").html("<i class='ace-icon fa fa-spinner orange'></i>");
- });*/
- function delFile(fileid){
- if(fileid!=undefined && fileid!="" && confirm('您确定要删除吗?')){
- $("#"+fileid).remove();
- var ids = $("#delFileIds").val();
- if(ids==undefined || ids==""){
- ids=fileid;
- $("#delFileIds").val(ids);
- }else{
- var tmp=ids+","+fileid;
- $("#delFileIds").val(tmp);
- }
- }
- }
|