Browse Source

显示上传文件进度

pull/16/head
hole-thu 3 years ago
parent
commit
bbb6e0db77
  1. 93
      src/UserAction.js

93
src/UserAction.js

@ -433,6 +433,9 @@ export class PostForm extends Component {
super(props); super(props);
this.state = { this.state = {
text: '', text: '',
upload_progress: '',
is_loading: false,
file_type: '',
cw: window.CW_BACKUP || '', cw: window.CW_BACKUP || '',
allow_search: window.AS_BACKUP || false, allow_search: window.AS_BACKUP || false,
loading_status: 'done', loading_status: 'done',
@ -574,51 +577,87 @@ export class PostForm extends Component {
on_file_change(event) { on_file_change(event) {
console.log(event); console.log(event);
let tar = event.target;
let f = event.target.files[0]; let f = event.target.files[0];
if (f) { if (f) {
tar.setAttribute('disabled', 'disabled'); this.setState({ is_loading: true, file_type: f.type });
let data = new FormData(); let data = new FormData();
data.append('file', f); data.append('file', f);
fetch(API_BASE + '/upload', { var xh = new XMLHttpRequest();
method: 'POST', xh.upload.addEventListener(
headers: { 'progress',
'User-Token': this.props.token, this.upload_progress.bind(this),
}, false,
body: data, );
}) xh.addEventListener('load', this.upload_complete.bind(this), false);
.then(get_json) xh.addEventListener('error', this.upload_error.bind(this), false);
.then((json) => { xh.addEventListener('abort', this.upload_abort.bind(this), false);
if (json.code !== 0) { xh.open('POST', API_BASE + '/upload');
throw new Error(json.msg); xh.setRequestHeader('User-Token', this.props.token);
xh.send(data);
}
} }
console.log(json);
update_text_after_upload(data) {
let url = let url =
(window.config.ipfs_gateway[0] || '<hash>(无ipfs网关)').replaceAll( (window.config.ipfs_gateway[0] || '<hash>(无ipfs网关)').replaceAll(
'<hash>', '<hash>',
json.data.hash, data.hash,
) + json.data.filename; ) + data.filename;
let new_text = let new_text =
this.state.text + this.state.text +
'\n' + '\n' +
(f.type.startsWith('image/') ? `![](${url})` : url); (this.state.file_type.startsWith('image/') ? `![](${url})` : url);
this.setState({ text: new_text }); this.setState({ text: new_text });
this.area_ref.current.set(new_text); this.area_ref.current.set(new_text);
tar.removeAttribute('disabled'); }
})
.catch((e) => { upload_progress(event) {
console.error(e); console.log(event.loaded, event.total);
alert('上传失败\n' + e); this.setState({
tar.removeAttribute('disabled'); upload_progress: `${((event.loaded * 100) / event.total).toFixed(2)}%`,
}); });
}
// event.target.value = null; upload_complete(event) {
try {
let j = JSON.parse(event.target.responseText);
if (j.code != 0) {
alert(j.msg);
throw new Error();
} }
this.update_text_after_upload(j.data);
this.setState({ is_loading: false });
} catch (e) {
console.log(e);
this.upload_error(event);
}
}
upload_error(event) {
alert(
'上传失败\n' +
(event.target.responseText.length < 100
? event.target.responseText
: event.target.status),
);
this.setState({ is_loading: false });
}
upload_abort(event) {
alert('上传已中断');
this.setState({ is_loading: false });
} }
render() { render() {
const { has_poll, poll_options, preview, loading_status } = this.state; const {
has_poll,
poll_options,
preview,
loading_status,
upload_progress,
is_loading,
} = this.state;
return ( return (
<form onSubmit={this.on_submit.bind(this)} className="post-form box"> <form onSubmit={this.on_submit.bind(this)} className="post-form box">
<div className="post-form-bar"> <div className="post-form-bar">
@ -694,7 +733,11 @@ export class PostForm extends Component {
type="file" type="file"
name="file" name="file"
onChange={this.on_file_change.bind(this)} onChange={this.on_file_change.bind(this)}
disabled={is_loading}
/> />
{is_loading && !!upload_progress && (
<small>上传中: {upload_progress}</small>
)}
<input <input
type="text" type="text"
placeholder="折叠警告(留空表示不折叠)" placeholder="折叠警告(留空表示不折叠)"

Loading…
Cancel
Save