hoa*_*oan 1 javascript file reactjs antd
我正在使用Ant Design Upload组件。有没有一种方法可以将选定文件的内容作为JavaScript中的字符串来显示在页面上?
理想情况下,我想访问file.data
什么。
<Upload
accept=".txt, .csv"
showUploadList={false}
beforeUpload={(file, fileList) => {
// Access file content here and do something with it
console.log(file);
// Prevent upload
return false;
}}
>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
Run Code Online (Sandbox Code Playgroud)
const { Upload, message, Button, Icon, } = antd;
const props = {
name: 'file',
action: '//jsonplaceholder.typicode.com/posts/',
headers: {
authorization: 'authorization-text',
},
onChange(info) {
if (info.file.status !== 'uploading') {
let reader = new FileReader();
reader.onload = (e) => {
console.log(e.target.result);
}
reader.readAsText(info.file.originFileObj);
}
if (info.file.status === 'done') {
message.success(`${info.file.name} file uploaded successfully`);
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
ReactDOM.render(
<Upload {...props}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>,
mountNode
);
Run Code Online (Sandbox Code Playgroud)
请检查CodePen
通过重启发这个从答案Shreyans Shrivastav但修改,以更好地适应了什么问。您可以使用FileReader
来读取文件的内容:
<Upload
accept=".txt, .csv"
showUploadList={false}
beforeUpload={file => {
const reader = new FileReader();
reader.onload = e => {
console.log(e.target.result);
};
reader.readAsText(file);
// Prevent upload
return false;
}}
>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1307 次 |
最近记录: |