【问题标题】:In electron, how to upload a file from it's full filename在电子中,如何从完整文件名上传文件
【发布时间】:2017-09-28 18:58:53
【问题描述】:

在我的电子应用程序中,我有一个按钮,上面写着“上传合同”。

单击时,它会从存储的合同的文件名中查看,即。 /home/users/filename.docx 并得到它(不需要对话窗口)。

现在,我在使用 axios 时将此文件作为多部分表单数据上传时遇到问题。

我读过关于this issue, asking for file upload on axios 的文章,这导致了this pull request for file upload on browserthis pull for uploading in node.js

我已经阅读了问题和 cmets 一点一点应用,但似乎无法使用 axios 正确上传真实文件。

这是我一直在做的。

作为浏览器上传

export function generateContract(fileUrl, customer) {
    const data = new FormData()
    const file = fs.createReadStream(fileUrl)

    data.append('names', customer.names)
    data.append('email', customer.email)
    data.append('contract', file)

    return data
}

//- In between generateContract & uploadFile function some function
//- gets the generatedContract data & push it to uploadFile for axios uploading.

//- In API file.

export function uploadFile(formData) {
    return checkConnetion()
        .then(() => {
            return axios.post(emailUrl, formData)
        })
        .catch(() => {
            return axios.post(emailUrlOffline, formData)
        })
}

在 generatedContract 中,我使用 FormData(我假设)是浏览器中的一个全局函数,用于创建用于上传文件的表单实例。

我也在使用fs.createReadStream,因为没有它我的文件上传看起来像一个包含三个字符串的对象(fileUrl 变成了一个普通字符串)。

但是,像这样上传并从本地服务器console.loging 我的请求正文我得到了绝望

{ names: 'Mwajuma Salmin',
  email: 'sammwaj@yahoo.com',
  contract: '[object Object]' }

合约文件是`[object Object]'。

作为 node.js 上传

一切都一样,除了我使用的是 npm 的form-data

输出是。

{ '[object FormData]': '' }

我在哪里搞砸了?

我尝试使用邮递员上传并手动选择要上传的文件作为表单数据和其他数据(电子邮件和姓名),一切都成功了!

输出。

{ names: 'maotora',
  contract:
   [ File {
       domain: null,
       _events: {},
       _eventsCount: 0,
       _maxListeners: undefined,
       size: 11609,
       path: '/tmp/upload_0af5c038e147888f0a7b89ad4784a4dc',
       name: 'Don Joe_08-06-17\'s_contract-for Plot 011.pdf',
       type: 'application/pdf',
       hash: null,
       lastModifiedDate: 2017-09-28T18:56:31.083Z,
       _writeStream: [Object] } ] }

我不能在应用程序中使用表单上传(只是上传按钮而不打开选择文件的对话框)我只有文件的文件名。

谢谢。

【问题讨论】:

  • 你搞清楚了吗?
  • 我做了,我用superagent只上传文件。

标签: javascript node.js file-upload electron axios


【解决方案1】:

这个问题已经有一段时间了,因为我无法用 axios 解决问题,不得不使用 superagent 一段时间。

但是,现在它是 solved 并且可以很好地使用 axios,这就是我在项目中解决它的方法。

//- Correctly placing a file_Url into FormData

export function generateContract(url, {names, email}) {
    const file = {
        uri: url,
        name: _.replace(url, /\.[doc,docx,odt,]+$/, ''),
        type: 'docx'
    }

    const formData = new FormData()
    formData.append('contract', file)
    formData.append('names', names)
    formData.append('email', email)

    return formData
}

//- 1. Calling generateContract method
//- 2. Passing the formdata (from method 1) to sendEmail

const contractData = generateContract(contractUrl, payload)
const response = yield sendEmail(contractData)


//- The sendEmail function that makes requests using axios

export function sendEmail(contractData) {
    return checkConnetion()
        .then(() => {
            return axios.post(emailUrl, contractData)
        })
        .catch(() => {
            // return axios.post(emailUrlOffline, contractData)
            userLog('Cannot connect to server, check your internet settings.', 'Connection Error', 'error')
        })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多