在电子中,如何从完整文件名上传文件
In electron, how to upload a file from it's full filename
在我的电子应用程序中,我有一个显示 "upload contract" 的按钮。
单击时,它会从存储的合同文件名中查找,即。 /home/users/filename.docx
并获取它(无需对话框 window)。
现在,我在使用 axios 时将此文件作为多部分表单数据上传时遇到问题。
我读过 this issue, asking for file upload on axios which led to this pull request for file upload on browser and this pull for uploading in node.js。
我已逐一阅读问题和评论,但似乎无法使用 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.log
从我的本地服务器发送我的请求正文,我感到绝望
{ names: 'Mwajuma Salmin',
email: 'sammwaj@yahoo.com',
contract: '[object Object]' }
合同文件是“[object Object]”。
上传为node.js
一切都一样,除了我使用的是 npm 的 form-data。
输出是。
{ '[object FormData]': '' }
我哪里搞砸了?
我尝试使用邮递员上传并手动 select 将文件上传为表单数据和其他数据(电子邮件和姓名),一切都成功了!
输出。
{ 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] } ] }
我无法在应用程序中使用表单上传(只需点击按钮即可上传,而无需打开 select 文件的对话框)我只有文件的文件名。
谢谢。
这个问题已经有一段时间了,因为我无法用 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')
})
}
在我的电子应用程序中,我有一个显示 "upload contract" 的按钮。
单击时,它会从存储的合同文件名中查找,即。 /home/users/filename.docx
并获取它(无需对话框 window)。
现在,我在使用 axios 时将此文件作为多部分表单数据上传时遇到问题。
我读过 this issue, asking for file upload on axios which led to this pull request for file upload on browser and this pull for uploading in node.js。
我已逐一阅读问题和评论,但似乎无法使用 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.log
从我的本地服务器发送我的请求正文,我感到绝望
{ names: 'Mwajuma Salmin',
email: 'sammwaj@yahoo.com',
contract: '[object Object]' }
合同文件是“[object Object]”。
上传为node.js
一切都一样,除了我使用的是 npm 的 form-data。
输出是。
{ '[object FormData]': '' }
我哪里搞砸了?
我尝试使用邮递员上传并手动 select 将文件上传为表单数据和其他数据(电子邮件和姓名),一切都成功了!
输出。
{ 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] } ] }
我无法在应用程序中使用表单上传(只需点击按钮即可上传,而无需打开 select 文件的对话框)我只有文件的文件名。
谢谢。
这个问题已经有一段时间了,因为我无法用 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')
})
}