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):
data = text_data or bytes_data
if data:
if data and self.chan:
data = json.loads(data)
# print('write: {!r}'.format(data))
resize = data.get('resize')

View File

@ -125,7 +125,7 @@ class FileManager extends React.Component {
formData.append('token', token);
formData.append('path', '/' + this.state.pwd.join('/'));
this.input.value = '';
http.post('/api/file/object/', formData, {timeout: 600000})
http.post('/api/file/object/', formData, {timeout: 600000, onUploadProgress: this._updateLocal})
.then(() => {
this.setState({uploadStatus: 'success'});
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 => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
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') {
this.socket.send('ping')
} else {
const percent = Number(e.data);
if (percent > this.state.percent) this.setState({percent});
const percent = this.state.percent + Number(e.data) / 2;
if (percent > this.state.percent) this.setState({percent: Number(percent.toFixed(1))});
if (percent === 100) {
this.socket.close()
}