简体   繁体   English

将本地文件从电子推送到服务器PHP

[英]Push Local File From Electron to Server PHP

js and electron. js和电子。 I have a problem to push my local images file to my remote server. 我有一个问题是将我的本地图像文件推送到我的远程服务器。 The server is using php. 服务器正在使用php。 Here's my client side code : 这是我的客户端代码:

 var documentList = []; var stmtDoc = db.prepare("SELECT * FROM `tb_dokumen_pasien`"); while(stmtDoc.step()) { var rowDoc = stmtDoc.getAsObject(); var data = fs.createReadStream(__dirname + "/resources/" + rowDoc['id'] + "/" + rowDoc['doc_name']); var document = { "document_name" : rowDoc['doc_name'], "data" : data } documentList.push(document); } 

And after that I create an ajax post request to my remote server : 然后我创建一个ajax post请求到我的远程服务器:

 $.ajax({ url: "http://url/web_api.php?action=test_upload", type: 'POST', data : { "document_list": documentList }, dataType: "json", success: function(response) { console.log(response); } }); 

When I test it, in my developer tools console I've got this message : 当我测试它时,在我的开发人员工具控制台中,我收到了以下消息:

internal/streams/BufferList.js:15 Uncaught TypeError: Cannot read property 'length' of undefined

And the file is not sent to the server. 并且文件不会发送到服务器。 Can anyone help me how can I solve this? 任何人都可以帮助我如何解决这个问题? Thanks 谢谢

for a while I found the working solution for this problem is use base64 format to send it to the remote server : 有一段时间我发现这个问题的工作解决方案是使用base64格式将它发送到远程服务器:

 var documentList = []; var stmtDoc = db.prepare("SELECT * FROM `tb_dokumen_pasien`"); while(stmtDoc.step()) { var rowDoc = stmtDoc.getAsObject(); var data = fs.createReadStream(__dirname + "/resources/" + rowDoc['id'] + "/" + rowDoc['doc_name']); var base64 = Buffer.from(data).toString('base64'); var document = { "document_name" : rowDoc['doc_name'], "data" : base64 } documentList.push(document); } 

And I still finding the best approach instead of using base64, Thanks 我仍然找到最好的方法,而不是使用base64,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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