简体   繁体   English

JavaScript通过$ _POST添加文件以形成数据

[英]JavaScript add file to form data via $_POST

In the following use case I'm trying to accomplish only step 3 is not working so far, and I'm working in a Joomla 3.4 environment as I'm working on a module here. 在下面的用例中我试图完成只有步骤3到目前为止还没有工作,我正在Joomla 3.4环境中工作,因为我正在这里处理一个模块。

  1. I have an html form asking for some input and a file upload. 我有一个html表单要求输入和文件上传。
  2. I process this file with JavaScript to pgp to encrypt it (with this: https://keybase.io/kbpgp ) 我用JavaScript处理这个文件到pgp来加密它(用这个: https//keybase.io/kbpgp
  3. Now I want to replace the original file in the form with the encrypted one, or at least add the encrypted one to the form data. 现在我想用加密的文件替换表单中的原始文件,或者至少将加密的文件添加到表单数据中。 This doesn't work so far, how can I accomplish that? 到目前为止这不起作用, 我怎么能做到这一点?
  4. When everything is done in the JavaScript part form.submit(); 当一切都在JavaScript部分中完成时, form.submit(); is called 叫做
  5. An email with the forms plain text encrypted as body text is sent and the file should be added as encrypted attachment. 将发送加密为正文的表单纯文本的电子邮件,并将该文件添加为加密附件。

Some details: 一些细节:

Sending the file unencrypted works quite well, so I thought I can just add the encrypted one, but apparently it doesn't work. 发送未加密的文件效果很好,所以我想我可以添加加密文件,但显然它不起作用。 The form submits the original content and nothing about the added file. 表单提交原始内容,而不是添加的文件。 I already searched for some solutions and this code snippet was the result of my research: 我已经搜索了一些解决方案,这个代码片段是我研究的结果:

var data = new FormData();
data.append('upload_file' , result_buffer);
var xhr = new XMLHttpRequest();
xhr.open( 'POST', '', true );
xhr.setRequestHeader('Content-Type', 'multipart/form-data'); 
xhr.send(data);

var form = document.getElementById('formencryptmessage');
form.submit();

upload_file is the form input of the original unencrypted file; upload_file是原始未加密文件的表单输入; the $_POST request keeps the original file no matter what I do; 无论我做什么,$ _POST请求都会保留原始文件; result_buffer is a binary buffer with files content; result_buffer是一个包含文件内容的二进制缓冲区; lastly, formencryptmessage is the form id and name. 最后, formencryptmessage是表单ID和名称。

Edit 编辑

The git repo with the code is here: https://github.com/JamborJan/joomlaencryptedcontact 代码的git repo在这里: https//github.com/JamborJan/joomlaencryptedcontact

We are talking about this: https://github.com/JamborJan/joomlaencryptedcontact/blob/master/tmpl/default.php 我们正在谈论这个: https//github.com/JamborJan/joomlaencryptedcontact/blob/master/tmpl/default.php

My main intent is to use only the browser session / RAM / hidden input field to do the file encryption and sending. 我的主要目的是只使用浏览器会话/ RAM /隐藏输入字段来进行文件加密和发送。 I didn't find a suitable solution so far. 到目前为止,我找不到合适的解决方案。

Maybe you could try to, once the file is pgp encrypted, to encode the datas in base64, then putting the resulting string in an input type=hidden by editing the DOM through JavaScript. 也许你可以尝试,一旦文件是pgp加密,在base64中编码数据,然后通过JavaScript编辑DOM将结果字符串放入输入类型=隐藏。

Your script which process the submitted form could then attach the file to the mail in an easy way, as mail attachements are base64 encoded. 然后,处理提交的表单的脚本可以以简单的方式将文件附加到邮件,因为邮件附件是base64编码的。 It would only have to read the value of the hidden input field. 它只需要读取隐藏输入字段的值。

If you want some example Javascript code, let me know, I've got some snippets dealing with this. 如果你想要一些示例Javascript代码,请告诉我,我有一些代码片段处理这个问题。

-> I was wondering the same thing as Mave, but I guess there's some reason if you want it this way? - >我想知道和Mave一样的东西,但我想如果你想要这样的话有一些原因吗? A workaround could be to create a new page between the one with the form, and the one which tells you the mail was sended (you could do what you want with the file, one the server side, and show it available for download to the user who just uploaded it unencrypted). 一种解决方法可能是在表单与表单之间创建一个新页面,并告诉您邮件已被发送(您可以使用该文件执行所需操作,一个服务器端,并显示它可供下载到刚刚上传未加密的用户)。

Another thing, to deal with base64 encoded datas in JavaScript, you can use: - btoa() method to encode to base64, - atob() method to reverse it. 另一件事,要在JavaScript中处理base64编码数据,你可以使用: - btoa()方法编码为base64, - atob()方法来反转它。

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

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