简体   繁体   中英

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. 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:

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. Without more info I'd recommend looking at: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM