在bootstrap-vue中如何将FileReader转换为blob并上传到服务器?

问题描述 投票:0回答:1

在@ vue / cli 4.1.1应用程序中,我使用bootstrap-vue和b-form-file组件上传图像

https://bootstrap-vue.js.org/docs/components/form-file/#multiple-files

具有定义:

<b-form-file
    id="upload_ad_image"
    v-model="new_upload_ad_image"
    :state="Boolean(new_upload_ad_image)"
    placeholder="Choose a file or drop it here..."
    drop-placeholder="Drop file here..."
    accept="image/jpeg, image/png, image/gif"
></b-form-file>

<div ref="uploaded_img_preview" id="uploaded_img_preview" class="m-2" >Uploaded image preview :</div>

我找到了代码段https://codepen.io/Tenderfeel/pen/rgqWXR并使用它在表单上显示选定的文件以进行预览。接下来,我需要将其上传到服务器上。我有使用:

这样的代码将图片上传为博客的经验
fetch(this.taskRow.imageFile.blob).then(function (response) {
    if (response.ok) {
        return response.blob().then(function (imageBlob) {
            let imageUploadData = new FormData()
            imageUploadData.append('id', self.taskRow.id)
            imageUploadData.append('image', imageBlob)
            imageUploadData.append('image_filename', self.taskRow.imageFile.name)

但是我需要将上传的图片转换为Blob。选择图像时使用方法:但出现错误:监视程序“ new_upload_ad_image”的回调错误:“ InvalidStateError:无法在'FileReader'上执行'readAsDataURL':对象已在忙于读取Blob

watch: {
    new_upload_ad_image(val) {
        if (!val) return;
        if (this.previewImg) {
            this.previewImg.remove();
        }
        const img = document.createElement("img");
        img.classList.add("obj");
        img.file = this.new_upload_ad_image;
        console.log('img.file::')
        console.log(img.file)

        this.previewImg = img;
        console.log('this.$refs.uploaded_img_preview::')
        console.log(this.$refs.uploaded_img_preview)

        console.log('img::')
        console.log(img)

        this.$refs.uploaded_img_preview.appendChild(img);

        const fileReader = new FileReader();
        fileReader.onload = (e) => {
            this.previewImg.src = e.target.result;
        };
        fileReader.readAsDataURL(this.new_upload_ad_image);
        console.log('fileReader::')
        console.log(fileReader)

        let blobObj= fileReader.readAsDataURL(img.file) // RAISE ERROR :

        console.log('blobObj::')
        console.log(blobObj)

    }
},

我在控制台中看到的是:https://imgur.com/a/2EZxq9C

如何获取Blob并将其上传到服务器上?

已修改块:

具有定义为id =“ upload_ad_image”的文件输入:

<b-form-file
    id="upload_ad_image"
    v-model="new_upload_ad_image"
    :state="Boolean(new_upload_ad_image)"
    placeholder="Choose a file or drop it here..."
    drop-placeholder="Drop file here..."
    accept="image/jpeg, image/png, image/gif"
></b-form-file>

我运行访存,发现图像Blob无效并且创建了文件,但是无效我有:

var self = this
const upload_ad_image = document.getElementById('upload_ad_image')
console.log('upload_ad_image::')
console.log(upload_ad_image)
console.log('upload_ad_image.files::')
console.log(upload_ad_image.files[0])

if (typeof upload_ad_image.files[0] == 'undefined') {
    self.showPopupMessage('Ad image upload', 'Invalid image !', 'warn')
    return
}
fetch(upload_ad_image.files[0].blob).then(function (response) {
    if (response.ok) {
        return response.blob().then(function (imageBlob) {
            console.log('imageBlob::')
            console.log(imageBlob) // Looks like this var has invalid content(printscreen below)!

            let imageUploadData = new FormData()
            imageUploadData.append('ad_id', self.editableAd.id)
            imageUploadData.append('main', self.new_upload_ad_image_main)
            imageUploadData.append('info', self.new_upload_ad_image_info)
            imageUploadData.append('image', imageBlob)
            imageUploadData.append('image_filename', upload_ad_image.files[0].name)

我在控制台中看到:https://imgur.com/a/4ees55C

什么地方有问题,如何解决?

"bootstrap-vue": "^2.3.0",
"vue": "^2.6.11",

谢谢!

blob filereader bootstrap-vue
1个回答
1
投票

File对象确实从Blob接口继承。

[Blob可以做的所有事情,File也可以做的事情,因此可以在代码中直接将this.new_upload_ad_image附加到FormData中。

const inp = document.getElementById('inp');
inp.onchange = (evt) => {
  const formData = new FormData();
  formData.append( 'my-file', inp.files[0], 'file.ext' );
  //  check it's correctly in the FormData
  console.log( [...formData.entries()] );
};
<input type="file" id="inp">
© www.soinside.com 2019 - 2024. All rights reserved.