【发布时间】:2019-03-21 13:58:54
【问题描述】:
我正在使用电子的对话框来访问用户的文件,现在我想将选定的文件上传到网络服务器。在我的代码中,我使用了fs.copyFile,但它向我显示了一个错误,因为它将项目路径添加到http://localhost/upload。
我会很高兴得到任何帮助。谢谢
dialog.showOpenDialog(dialogOptions,function(fileNames) {
// fileNames is an array that contains all the selected
if (fileNames === undefined) {
console.log("No file selected");
} else {
readFile(fileNames[0]);
}
});
function readFile(filepath) {
fs.readFile(filepath, 'utf-8', (err, data) => {
if (err) {
alert("An error ocurred reading the file :" + err.message)
return
}
fileName = pathf.basename(filepath);
// Copy the chosen file to the application's data path
fs.copyFile(filepath, 'http://localhost/upload/' + fileName, (err) => {
if (err) throw err;
});
// handle the file content
event.sender.send('fileData', data)
event.sender.send('fileDataPath', filepath)
})
}
【问题讨论】:
-
您是否有一个 Web 服务器在本地侦听某个特定端口,并且有一个 API 端点等待您的上传请求?
-
是的。像这样 require('http').createServer(function (request, res) { request.addListener('end', function () { file.serve(request, res) res.writeHead(200, { 'Content-Type' : 'text/html' }); res.write('') ; return res.end(); }).resume() }).listen(9990)
标签: javascript node.js localhost electron