Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit e88ba4b

Browse files
authored
Merge pull request #561 from PSoul/master
fix #542
2 parents 6bdafa2 + ca657b5 commit e88ba4b

File tree

4 files changed

+319
-23
lines changed

4 files changed

+319
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* jQuery File Upload Processing Plugin
3+
* https://github.com/blueimp/jQuery-File-Upload
4+
*
5+
* Copyright 2012, Sebastian Tschan
6+
* https://blueimp.net
7+
*
8+
* Licensed under the MIT license:
9+
* https://opensource.org/licenses/MIT
10+
*/
11+
12+
/* jshint nomen:false */
13+
/* global define, require, window */
14+
15+
;(function (factory) {
16+
'use strict';
17+
if (typeof define === 'function' && define.amd) {
18+
// Register as an anonymous AMD module:
19+
define([
20+
'jquery',
21+
'./jquery.fileupload'
22+
], factory);
23+
} else if (typeof exports === 'object') {
24+
// Node/CommonJS:
25+
factory(
26+
require('jquery'),
27+
require('./jquery.fileupload')
28+
);
29+
} else {
30+
// Browser globals:
31+
factory(
32+
window.jQuery
33+
);
34+
}
35+
}(function ($) {
36+
'use strict';
37+
38+
var originalAdd = $.blueimp.fileupload.prototype.options.add;
39+
40+
// The File Upload Processing plugin extends the fileupload widget
41+
// with file processing functionality:
42+
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
43+
44+
options: {
45+
// The list of processing actions:
46+
processQueue: [
47+
/*
48+
{
49+
action: 'log',
50+
type: 'debug'
51+
}
52+
*/
53+
],
54+
add: function (e, data) {
55+
var $this = $(this);
56+
data.process(function () {
57+
return $this.fileupload('process', data);
58+
});
59+
originalAdd.call(this, e, data);
60+
}
61+
},
62+
63+
processActions: {
64+
/*
65+
log: function (data, options) {
66+
console[options.type](
67+
'Processing "' + data.files[data.index].name + '"'
68+
);
69+
}
70+
*/
71+
},
72+
73+
_processFile: function (data, originalData) {
74+
var that = this,
75+
dfd = $.Deferred().resolveWith(that, [data]),
76+
chain = dfd.promise();
77+
this._trigger('process', null, data);
78+
$.each(data.processQueue, function (i, settings) {
79+
var func = function (data) {
80+
if (originalData.errorThrown) {
81+
return $.Deferred()
82+
.rejectWith(that, [originalData]).promise();
83+
}
84+
return that.processActions[settings.action].call(
85+
that,
86+
data,
87+
settings
88+
);
89+
};
90+
chain = chain.then(func, settings.always && func);
91+
});
92+
chain
93+
.done(function () {
94+
that._trigger('processdone', null, data);
95+
that._trigger('processalways', null, data);
96+
})
97+
.fail(function () {
98+
that._trigger('processfail', null, data);
99+
that._trigger('processalways', null, data);
100+
});
101+
return chain;
102+
},
103+
104+
// Replaces the settings of each processQueue item that
105+
// are strings starting with an "@", using the remaining
106+
// substring as key for the option map,
107+
// e.g. "@autoUpload" is replaced with options.autoUpload:
108+
_transformProcessQueue: function (options) {
109+
var processQueue = [];
110+
$.each(options.processQueue, function () {
111+
var settings = {},
112+
action = this.action,
113+
prefix = this.prefix === true ? action : this.prefix;
114+
$.each(this, function (key, value) {
115+
if ($.type(value) === 'string' &&
116+
value.charAt(0) === '@') {
117+
settings[key] = options[
118+
value.slice(1) || (prefix ? prefix +
119+
key.charAt(0).toUpperCase() + key.slice(1) : key)
120+
];
121+
} else {
122+
settings[key] = value;
123+
}
124+
125+
});
126+
processQueue.push(settings);
127+
});
128+
options.processQueue = processQueue;
129+
},
130+
131+
// Returns the number of files currently in the processsing queue:
132+
processing: function () {
133+
return this._processing;
134+
},
135+
136+
// Processes the files given as files property of the data parameter,
137+
// returns a Promise object that allows to bind callbacks:
138+
process: function (data) {
139+
var that = this,
140+
options = $.extend({}, this.options, data);
141+
if (options.processQueue && options.processQueue.length) {
142+
this._transformProcessQueue(options);
143+
if (this._processing === 0) {
144+
this._trigger('processstart');
145+
}
146+
$.each(data.files, function (index) {
147+
var opts = index ? $.extend({}, options) : options,
148+
func = function () {
149+
if (data.errorThrown) {
150+
return $.Deferred()
151+
.rejectWith(that, [data]).promise();
152+
}
153+
return that._processFile(opts, data);
154+
};
155+
opts.index = index;
156+
that._processing += 1;
157+
that._processingQueue = that._processingQueue.then(func, func)
158+
.always(function () {
159+
that._processing -= 1;
160+
if (that._processing === 0) {
161+
that._trigger('processstop');
162+
}
163+
});
164+
});
165+
}
166+
return this._processingQueue;
167+
},
168+
169+
_create: function () {
170+
this._super();
171+
this._processing = 0;
172+
this._processingQueue = $.Deferred().resolveWith(this)
173+
.promise();
174+
}
175+
176+
});
177+
178+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* jQuery File Upload Validation Plugin
3+
* https://github.com/blueimp/jQuery-File-Upload
4+
*
5+
* Copyright 2013, Sebastian Tschan
6+
* https://blueimp.net
7+
*
8+
* Licensed under the MIT license:
9+
* https://opensource.org/licenses/MIT
10+
*/
11+
12+
/* global define, require, window */
13+
14+
;(function (factory) {
15+
'use strict';
16+
if (typeof define === 'function' && define.amd) {
17+
// Register as an anonymous AMD module:
18+
define([
19+
'jquery',
20+
'./jquery.fileupload-process'
21+
], factory);
22+
} else if (typeof exports === 'object') {
23+
// Node/CommonJS:
24+
factory(
25+
require('jquery'),
26+
require('./jquery.fileupload-process')
27+
);
28+
} else {
29+
// Browser globals:
30+
factory(
31+
window.jQuery
32+
);
33+
}
34+
}(function ($) {
35+
'use strict';
36+
37+
// Append to the default processQueue:
38+
$.blueimp.fileupload.prototype.options.processQueue.push(
39+
{
40+
action: 'validate',
41+
// Always trigger this action,
42+
// even if the previous action was rejected:
43+
always: true,
44+
// Options taken from the global options map:
45+
acceptFileTypes: '@',
46+
maxFileSize: '@',
47+
minFileSize: '@',
48+
maxNumberOfFiles: '@',
49+
disabled: '@disableValidation'
50+
}
51+
);
52+
53+
// The File Upload Validation plugin extends the fileupload widget
54+
// with file validation functionality:
55+
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
56+
57+
options: {
58+
/*
59+
// The regular expression for allowed file types, matches
60+
// against either file type or file name:
61+
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
62+
// The maximum allowed file size in bytes:
63+
maxFileSize: 10000000, // 10 MB
64+
// The minimum allowed file size in bytes:
65+
minFileSize: undefined, // No minimal file size
66+
// The limit of files to be uploaded:
67+
maxNumberOfFiles: 10,
68+
*/
69+
70+
// Function returning the current number of files,
71+
// has to be overriden for maxNumberOfFiles validation:
72+
getNumberOfFiles: $.noop,
73+
74+
// Error and info messages:
75+
messages: {
76+
maxNumberOfFiles: 'Maximum number of files exceeded',
77+
acceptFileTypes: 'File type not allowed',
78+
maxFileSize: 'File is too large',
79+
minFileSize: 'File is too small'
80+
}
81+
},
82+
83+
processActions: {
84+
85+
validate: function (data, options) {
86+
if (options.disabled) {
87+
return data;
88+
}
89+
var dfd = $.Deferred(),
90+
settings = this.options,
91+
file = data.files[data.index],
92+
fileSize;
93+
if (options.minFileSize || options.maxFileSize) {
94+
fileSize = file.size;
95+
}
96+
if ($.type(options.maxNumberOfFiles) === 'number' &&
97+
(settings.getNumberOfFiles() || 0) + data.files.length >
98+
options.maxNumberOfFiles) {
99+
file.error = settings.i18n('maxNumberOfFiles');
100+
} else if (options.acceptFileTypes &&
101+
!(options.acceptFileTypes.test(file.type) ||
102+
options.acceptFileTypes.test(file.name))) {
103+
file.error = settings.i18n('acceptFileTypes');
104+
} else if (fileSize > options.maxFileSize) {
105+
file.error = settings.i18n('maxFileSize');
106+
} else if ($.type(fileSize) === 'number' &&
107+
fileSize < options.minFileSize) {
108+
file.error = settings.i18n('minFileSize');
109+
} else {
110+
delete file.error;
111+
}
112+
if (file.error || data.files.error) {
113+
data.files.error = true;
114+
dfd.rejectWith(this, [data]);
115+
} else {
116+
dfd.resolveWith(this, [data]);
117+
}
118+
return dfd.promise();
119+
}
120+
121+
}
122+
123+
});
124+
125+
}));

cobra/templates/asset/js/jquery.fileupload.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,9 @@
11221122
}, errorHandler);
11231123
}
11241124
} else if (entry.isDirectory) {
1125-
dirReader = entry.createReader();
1126-
readEntries();
1125+
$('#progress').empty();
1126+
$('#progress').html('禁止文件夹上传!');
1127+
return;
11271128
} else {
11281129
// Return an empy list for file system items
11291130
// other than files or directories:

cobra/templates/index.html

+13-21
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ <h3>Select a compressed file scan</h3>
102102
<h4><span>Drag and drop</span> upload</h4>
103103
<div align="center" id="selectFile"></div>
104104
<hr>
105-
<input type="file" class="btn_1" id="uploadFile" name="file" style="width: 100%;">
105+
<input type="file" class="btn_1" id="uploadFile" name="file" style="width: 100%;" accept="application/bacnet-xdd+zip,.tar,.gz,.rar,.zip,.tgz,.bz2">
106+
<br>
106107
<small>Support for upload extensions: tar.bz2|tar|gz|tgz|tar.gz|rar|zip</small>
108+
<small>只允许单文件上传,不允许文件夹拖拽</small>
109+
107110
<div id="progress">
108111
<div class="bar"></div>
109112
</div>
@@ -125,31 +128,15 @@ <h4><span>Drag and drop</span> upload</h4>
125128
<script src="{{ url_for('static', filename='js/jquery.ui.widget.js') }}"></script>
126129
<script src="{{ url_for('static', filename='js/jquery.iframe-transport.js') }}"></script>
127130
<script src="{{ url_for('static', filename='js/jquery.fileupload.js') }}"></script>
131+
<script src="{{ url_for('static', filename='js/jquery.fileupload-process.js') }}"></script>
132+
<script src="{{ url_for('static', filename='js/jquery.fileupload-validate.js') }}"></script>
128133
<script>
129134

130-
// drag and drop upload file
131-
// prevent browser default action
132-
$(document).on({
133-
dragleave: function (e) {
134-
e.preventDefault();
135-
},
136-
drop: function (e) {
137-
e.preventDefault();
138-
},
139-
dragenter: function (e) {
140-
e.preventDefault();
141-
},
142-
dragover: function (e) {
143-
e.preventDefault();
144-
}
145-
});
146-
135+
var fileList;
147136
var dropbox = document.getElementById('dropbox');
148-
var fileList = null;
149137
dropbox.addEventListener("drop", function (e) {
150138
e.preventDefault();
151139
fileList = e.dataTransfer.files;
152-
$('#selectFile').html(fileList[0].name);
153140
});
154141

155142
// click and select file to upload
@@ -161,7 +148,6 @@ <h4><span>Drag and drop</span> upload</h4>
161148
}
162149

163150
$(function () {
164-
165151
// Tab1
166152
$("#scan").on('click', function (e) {
167153
e.preventDefault();
@@ -229,6 +215,12 @@ <h4><span>Drag and drop</span> upload</h4>
229215
$('#uploadFile').fileupload({
230216
url: '/api/upload',
231217
dataType: 'json',
218+
acceptFileTypes: /(\.|\/)(bz2|tar|gz|tgz|rar|zip)$/i,
219+
maxNumberOfFiles: 1,
220+
processfail: function (e, result) {
221+
$('#progress').empty();
222+
$('#progress').html('文件类型错误!');
223+
},
232224
done: function (e, result) {
233225
if (result.result.code === 1001) {
234226
var sid = result.result.result.sid;

0 commit comments

Comments
 (0)