[英]Electron Dialog send file to server with vue resource
我正在構建一個處理文件上傳的電子應用程序,正在使用對話框從用戶那里獲取文件,我需要將文件發送到服務器,但是我正在獲取文件路徑,但是在發送文件時出現錯誤。 我正在使用Vue資源進行請求。 下面是我的代碼:
<template>
<div>
<button @click="uploadAct()" class="primary">New Upload </button>
</div>
</template>
<script>
const {dialog} = require('electron').remote
const fs = require('fs')
import reqApi from '../../api/something'
export default {
methods: {
uploadAct () {
dialog.showOpenDialog({
title: 'Upload Attachments',
buttonLabel: 'Upload',
filters: [
{name: 'Images', extensions: ['jpg', 'png', 'gif']},
{name: 'All Files', extensions: ['*']}
],
properties: ['openFile', 'multiSelections']
}, function (filenames) {
if (filenames) {
let d = ''
filenames.forEach(function (element) {
d = element
})
// here i get a path of file correctly something like /path/to/file.jpg
reqApi.uploadattachmnets({photo: fs.createReadStream(d)}).then(
(response) => {
console.log(response)
},
(error) => {
console.log(error)
})
// })
}
})
}
}
}
</script>
但是,我最終在請求中出錯,將不勝感激。
可能是拼寫錯誤,但是您可以調用API:
carApi.uploadattachmnets({photo: fs.createReadStream(d)})
這與您要導入的內容不同:
import reqApi from '../../api/something'
如果不是上述情況,我認為如果Postman已經能夠發送文件並從端點接收正確的響應,那么這將是CORS問題。 如果沒有更多信息,我建議您查看: https : //www.html5rocks.com/en/tutorials/cors/#toc-making-a-cors-request
要獲得更具體的響應,您需要發布API代碼,以便我們查看您如何發送文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.