英文:
How to upload file in NodeJs to PocketBase
问题
我正在开展一个项目,需要将许多PDF文件上传到PocketBase集合。
我已经将所有文件存储在我的计算机上,想要使用Node.js和PocketBase JavaScript SDK进行上传。PocketBase希望我将文件发送为
,但在Node.js中这是不可能的。理想情况下,代码应该类似于这样:
const fileObject = loadFile(pathToFile);
const entry = {
nameField: "some-field",
fileField: fileObject
}
await pb.collection("my-collection").create(entry)
我没有找到任何可以帮助创建loadFile
函数的代码段。
英文:
I'm working on a project where I need to upload many PDF files to a PocketBase collection.
I have all the files on my computer and I'd like to upload them using nodejs and the PocketBase JavaScript SDK. PocketBase expects me to send the file as a
, which is not possible in nodejs.Ideally the code would look something like that:
const fileObject = loadFile(pathToFile);
const entry = {
nameField: "some-field",
fileField: fileObject
}
await pb.collection("my-collection").create(entry)
I didn't find any bits of code that could help creating a loadFile
function.
答案1
得分: 1
你需要在上传文件到pocketbase时将你的表单以multipart/form-data
格式发送。
尝试:
const res = fetch(
"http://127.0.0.1:8090/api/collections/my-collection/records",
{
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
},
body: myFormWhichHasFiles,
}
)
另外,请确保在使用multipart/form-data
时不要对你的表单使用JSON.stringify
。
专业提示:如果不设置'Content-type',它应该默认为multipart/form-data
。
英文:
You are supposed to send your form as multipart/form-data
when uploading files to pocketbase.
Try:
const res = fetch(
"http://127.0.0.1:8090/api/collections/my-collection/records",
{
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
},
body: myFormWhichHasFiles,
}
)
Also, make sure you don't use JSON.stringify
on your form when using multipart/form data
.
Pro tip: if you leave out 'Content-type', it should default to multipart/form-data
.
答案2
得分: 0
你现在可以通过将SDK升级到0.17或更高版本,并发送不带问题的文件对象来完成。
英文:
you can do it now by updating your SDK to 0.17 or more and sending File Object with no problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论