'RequestError: Error: Invalid URI' when uploading file to parse server cloud

'RequestError: Error: Invalid URI' when uploading file to parse server cloud

我在我的电子应用程序 main.js 中使用来自解析 javascript sdk 的示例代码,它与示例 URI 一起工作正常,但是当我使用我自己的 URI 时,它是我的应用程序路由级别的本地文件,我收到以下 RequestError 消息,

Error: Invalid URI.

我尝试使用的 URI 是 file://${__dirname}/tempInv.pdf 我已经尝试了我能想到的文件地址的所有不同变体,但结果仍然相同。是否有某种指向本地主机的方法可能会克服此错误。该应用程序将被部署,因此需要跨平台工作。

const request = require('request-promise');
const Parse = require('parse/node');

....

const options = {
  uri: 'https://-----/-----',
  resolveWithFullResponse: true,
  encoding: null, // <-- this is important for binary data like images.
};

request(options)
  .then((response) => {
    const data = Array.from(Buffer.from(response.body, 'binary'));
    const contentType = response.headers['content-type'];
    const file = new Parse.File('logo', data, contentType);
    return file.save();
  })
  .then((file => console.log(file.url())))
  .catch(console.error);
const Parse = require('parse/node');

const express = require('express');
const XeroClient = require('xero-node').AccountingAPIClient;
const config = require('./config.json');

const fs  = require('fs');

const request = require('request-promise');

let appEx = express();

let lastRequestToken = null;
let xeroClient = new XeroClient(config);


appEx.set('port', 3000)

ipcMain.on('message-createInvoice', function (event, data) {

    let win = new BrowserWindow({ width: 340, height: 480 });
        win.setPosition(0, 0);
        win.loadURL('http://localhost:3000/');

    appEx.get('/', async function(req, res) {

        lastRequestToken = await xeroClient.oauth1Client.getRequestToken();
        let authoriseUrl = xeroClient.oauth1Client.buildAuthoriseUrl(lastRequestToken);

        res.redirect(authoriseUrl);

    })   


    appEx.get('/callback', async function(req, res) {

        let oauth_verifier = req.query.oauth_verifier;
        let accessToken = await xeroClient.oauth1Client.swapRequestTokenforAccessToken(lastRequestToken, oauth_verifier)
            .then(async() => {
                var invoice = await xeroClient.invoices.create(data)           
                    .then((invoice) => {
                        var inv = invoice["Invoices"][0];
                        var invId = inv["InvoiceID"];

                        xeroClient.invoices.savePDF({ InvoiceID: invId, savePath: path.join(__dirname, '../temp-files/temp-inv.pdf',)})
                            .then(() => {

                                Parse.initialize("--appId--");    
                                Parse.serverURL = 'http://--.---.--.---:80/parse';

                                const data = fs.readFileSync(path.join(__dirname, '../temp-files/temp-inv.pdf'));

                                const file = await new Parse.File('inv.pdf', [...data]);
                                return file.save()
                                    .then((file) => {
                                        var Invoices = Parse.Object.extend("Invoices");
                                        var invoice = new Invoices;
                                        invoice.set('invPdf', file);
                                        invoice.save();     
                                        event.returnValue = true;
                                        win.close();
                                    })
                                    .catch((err) => {
                                    console.log('Failed to save invoice to Parse Cloud: ' + err);
                                    event.returnValue = false;
                                    win.close();
                                    })

                            })
                            .catch((err) => {
                            console.log('Failed to save invoice to temp-file: ' + err);
                            event.returnValue = false;
                            win.close();
                            })
                    })
                    .catch((err) => {
                    console.log('Failed to create invoice in xero' + err);
                    event.returnValue = false;
                    win.close();
                    })
            })
            .catch((err) => {
                console.log('Oauth failed' + err);
                event.returnValue = false;
                win.close();
            })
    })

    event.sender.send('message-createInvoice', true);

});


appEx.listen(appEx.get('port'), function() {
    console.log("Your Xero basic public app is running at http://localhost:" + appEx.get('port'))
})

我认为 request-promise 不是读取本地文件的最佳方式。我建议你使用 fs 包。它会是这样的:

const path = require('path');
const fs = require('fs');
const Parse = require('parse/node');

....

const data = fs.readFileSync(path.join(__dirname, './tempInv.pdf'));
const file = new Parse.File('logo.pdf', [...data]);
file.save()
  .then(file => console.log(file.url()))
  .catch(console.error);