[英]Electron Dialog send file to server with vue resource
I am building an electron app which handles file uploads, I am using dialog to get the files from user, I need to send the files to server but I am getting the files path but I get errors when sending them . 我正在构建一个处理文件上传的电子应用程序,正在使用对话框从用户那里获取文件,我需要将文件发送到服务器,但是我正在获取文件路径,但是在发送文件时出现错误。 I am using Vue resource for requests.
我正在使用Vue资源进行请求。 Below is my code:
下面是我的代码:
<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>
I however end up with error on the request , any help will be appreciated . 但是,我最终在请求中出错,将不胜感激。
Probably a typo but you have a call to an API: 可能是拼写错误,但是您可以调用API:
carApi.uploadattachmnets({photo: fs.createReadStream(d)})
which is different to the one you are importing: 这与您要导入的内容不同:
import reqApi from '../../api/something'
If not the above I'd assume this is going to be a CORS issue if Postman is already able to send files and receive the correct response from the endpoint. 如果不是上述情况,我认为如果Postman已经能够发送文件并从端点接收正确的响应,那么这将是CORS问题。 Without more info I'd recommend looking at: https://www.html5rocks.com/en/tutorials/cors/#toc-making-a-cors-request
如果没有更多信息,我建议您查看: https : //www.html5rocks.com/en/tutorials/cors/#toc-making-a-cors-request
For a more specific response you'd need to post the API code so we can review how you are sending the file. 要获得更具体的响应,您需要发布API代码,以便我们查看您如何发送文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.