javascript 我如何上传文件到pocketbase?

hrirmatl  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(105)

我正在使用pocketbase和svelte.js。我如何通过html输入上传图片到pocketbase。

<script lang="ts">
    import { currentUser, pb } from './pocketbase';
    const axios = require('axios').default;

    const fileInput = document.getElementById('fileInput');

    async function uploadFile() {
        axios({
            method: 'post',
            url: 'http://127.0.0.1:8090/api/collections/images/records',
            data: {
                image: fileInput.file,
            }
        });
    }

</script>

<form enctype="multipart/form-data" method="post" on:submit={uploadFile}>
    <input type="file" name="fileInput" id="fileInput">
    <button type="submit">Upload</button>
</form>
pepwfjgg

pepwfjgg1#

您需要使用FormData()示例而不是JSON。
示例:

let formData = new FormData();
        
formData.append('image', image); //here, image is an uploaded Image object

await pb.collection('posts').create(formData) //replace data accordingly

添加后,您可以使用记录创建/更新API发送multipart/form-data请求来创建/更新记录并上载"文档"文件。
Source

相关问题