file - this.$refs.selectedImages.files.map 不是一个函数 - vue electro

标签 file vue.js electron

我正在尝试在我的 vue 组件中使用 map 函数。我有一个可以接受多个文件的文件上传输入。由于我正在使用 Electron ,我无法直接将文件传递到应用程序的后端,因此我想使用 Array.prototype.map() 函数来获取文件路径并处理它们使用 ipcRenderer.send() 函数传递数组后。不管怎样,我总是收到这个错误this.$refs.selectedImages.files.map is not a function我该如何修复它?

这是我正在尝试的实际代码

    handleFiles() {
      this.files = this.$refs.selectedImages.files.map( (file) => file.path ); //this will cause the error
    }, //
    processFiles() {
      this.isLoading = true;
    
      window.ipcRenderer.send('processFiles', this.files);
      
      window.ipcRenderer.on('', (event, data) => {
        console.log(ecent, data);
        saveAs(data, 'processed.mfs')
      });
    } 

更新

为了尝试解决该问题,我循环 FilesList 以提取每个文件的路径,然后使用 ipcRenderer 传递它。问题是我无法在 background.js 上获取消息,我也不知道为什么。

this.$refs.selectedImages.files.forEach( (file) => {
 this.files.push(file.path) // this will push into the array the path of each file
})

window.ipcRenderer.send('processFiles', this.files);

//background.js

import { ipcMain } from 'electron'

ipcMain.on('processFiles', (event, data) => {
 console.log(event, data) // the message will not arrive
})

最佳答案

FileList object 实际上不是数组,并且没有 .map 方法。但您可以像这样访问文件:

const files = this.$refs.selectedImages.files;

for (var i = 0; i < files.length; i++) {
   console.log(files[i]);
}

关于file - this.$refs.selectedImages.files.map 不是一个函数 - vue electro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65156175/

相关文章:

c# - 如何在 .NET 中远程逐行读取文件?

C编程指针数组读取文件

css - Vue JS v-for 生成的组件不延伸座高(overflow-y)

javascript - 如何修复 Cordova Android 模拟器问题?

javascript - 在 Electron.js 中打开文件

ios - 使用在后台运行的应用程序打开文件

java - 使用java,从文件夹位置提取列表文件名的最资源有效的方法是什么

javascript - 绑定(bind)类发生一次

javascript - electron App中如何获取appId

angular - “在 Angular 区域外触发导航”解释