gpt4 book ai didi

reactjs - 如何使用 PrimeReact FileUpload 组件上传文件

转载 作者:行者123 更新时间:2023-12-04 10:31:50 29 4
gpt4 key购买 nike

我是 React/Spring 初学者,我不知道如何使用 Primereact 制作文件上传。
他们的文档说明如下( https://www.primefaces.org/primereact/#/fileupload ):

FileUpload requires a url property as the upload target and a name to identify the files at backend.


<FileUpload name="demo" url="./upload"></FileUpload>
但它既没有说我如何获取数据,我如何访问后端的“演示”,也没有说我得到什么样的响应。

有人可以帮我吗?我已经在网上搜索过,但没有找到任何东西。

最佳答案

你可以这样做:

<FileUpload name="invoice"
accept="image/*"
customUpload={true}
uploadHandler={invoiceUploadHandler}
mode="basic"
auto={true}
chooseLabel="Upload invoice"/>
const invoiceUploadHandler = ({files}) => {
const [file] = files;
const fileReader = new FileReader();
fileReader.onload = (e) => {
uploadInvoice(e.target.result);
};
fileReader.readAsDataURL(file);
};
像这样发送您的请求
const uploadInvoice = async (invoiceFile) => {
let formData = new FormData();
formData.append('invoiceFile', invoiceFile);

const response = await fetch(`orders/${orderId}/uploadInvoiceFile`,
{
method: 'POST',
body: formData
},
);
};
重要提示:不要设置任何 Content-Type头!这将自动完成。

关于reactjs - 如何使用 PrimeReact FileUpload 组件上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60389073/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com