简体   繁体   English

如何以编程方式在node-webkit / nw.js中上传文件

[英]How to programmatically upload file in node-webkit / nw.js

I am using node-webkit/nw.js as an automation tool to test my web application. 我正在使用node-webkit / nw.js作为自动化工具来测试我的Web应用程序。 My application has a record mode to capture all click events and keyboard events. 我的应用程序具有记录模式,可以捕获所有单击事件和键盘事件。 All the captured events are logged into a file and while in play mode, the logged events are run from clean state. 所有捕获的事件都记录到文件中,并且在播放模式下,记录的事件从干净状态运行。

However, since file uploads open up a File dialog, I cannot emulate a file selection. 但是,由于文件上传会打开“文件”对话框,因此我无法模拟文件选择。 Since am using node webkit, I can programmatically read a file from the user system and upload it as blob using FormData and Blob . 由于使用的是node webkit,因此我可以以编程方式从用户系统中读取文件,并使用FormDataBlob将其作为blob上传。 However, based on the file upload, there are events that have to be triggered, and after the upload is complete other fields have to populated say with an image. 但是,基于文件上传,必须触发一些事件,上传完成后,必须使用图像填充其他字段。

Is there a way I can attach a file in the form data programmatically? 有没有办法以编程方式在表单数据中附加文件? I can understand it is not possible in a normal browser since it would be a security issue. 我可以理解,在普通浏览器中是不可能的,因为这将是一个安全问题。 In a node webkit environment am anyways able to access and upload user file in the background. 在节点Webkit环境中,无论如何都可以在后台访问和上传用户文件。 So, will it be possible to modify the input file value or by some other means add a blob inside the Form and upload the file? 因此,是否可以修改输入文件的值或通过其他方式在Form内添加blob并上传文件?

It seems the steps involved is elaborated in the github page of nw.js but not on docs. 似乎所涉及的步骤在nw.js的github页上进行了详细说明,而在文档中未进行详细说明。

Steps to get reference of form node and input node. 获取表单节点和输入节点的参考的步骤。

//Detect if button clicked is inside a form. 
//In my case all attachments use same component. So I know the no. of levels to move up
if(activeElm.parentNode.parentNode.parentNode.tagName=="FORM"){
    //Navigate to input elm and save it as new activeElm. 
    //Navigation will differ based on your page.
    activeElm = activeElm.parentNode.parentNode.parentNode.getElementsByTagName('input')[0];
}

Steps to emulate file attachment. 模拟文件附件的步骤。

var f = new File('/path/to/file', 'name');
 var files = new FileList();
 files.append(f);
 elm.files = files;

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

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