U 优化文件管理器进度条

pull/410/head
vapao 2021-11-25 15:07:46 +08:00
parent 2b33df0765
commit fbd3255540
2 changed files with 9 additions and 4 deletions

View File

@ -97,7 +97,7 @@ class SSHConsumer(WebsocketConsumer):
def receive(self, text_data=None, bytes_data=None): def receive(self, text_data=None, bytes_data=None):
data = text_data or bytes_data data = text_data or bytes_data
if data: if data and self.chan:
data = json.loads(data) data = json.loads(data)
# print('write: {!r}'.format(data)) # print('write: {!r}'.format(data))
resize = data.get('resize') resize = data.get('resize')

View File

@ -125,7 +125,7 @@ class FileManager extends React.Component {
formData.append('token', token); formData.append('token', token);
formData.append('path', '/' + this.state.pwd.join('/')); formData.append('path', '/' + this.state.pwd.join('/'));
this.input.value = ''; this.input.value = '';
http.post('/api/file/object/', formData, {timeout: 600000}) http.post('/api/file/object/', formData, {timeout: 600000, onUploadProgress: this._updateLocal})
.then(() => { .then(() => {
this.setState({uploadStatus: 'success'}); this.setState({uploadStatus: 'success'});
this.fetchFiles() this.fetchFiles()
@ -134,6 +134,11 @@ class FileManager extends React.Component {
} }
}; };
_updateLocal = (e) => {
const percent = e.loaded / e.total * 100 / 2
this.setState({percent: Number(percent.toFixed(1))})
}
_updatePercent = token => { _updatePercent = token => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
this.socket = new WebSocket(`${protocol}//${window.location.host}/api/ws/exec/${token}/?x-token=${X_TOKEN}`); this.socket = new WebSocket(`${protocol}//${window.location.host}/api/ws/exec/${token}/?x-token=${X_TOKEN}`);
@ -142,8 +147,8 @@ class FileManager extends React.Component {
if (e.data === 'pong') { if (e.data === 'pong') {
this.socket.send('ping') this.socket.send('ping')
} else { } else {
const percent = Number(e.data); const percent = this.state.percent + Number(e.data) / 2;
if (percent > this.state.percent) this.setState({percent}); if (percent > this.state.percent) this.setState({percent: Number(percent.toFixed(1))});
if (percent === 100) { if (percent === 100) {
this.socket.close() this.socket.close()
} }