使用CefSharp和SchemeHandler如何创建Javascript File() [英] Using CefSharp and a SchemeHandler how to create a Javascript File()
问题描述
在我要借助Cefsharp自动化的网站上,我需要提供一个JavaScript File.File()。我要提供的文件已本地保存,可以是pdf,office文档或图像等任何文件。就CefSharp而言,我已经实现了ISchemeHandlerFactory和ResourceHandler添加了test://方案,例如,我已经成功添加了这样的JS文件。
On a website I am automating with the help of Cefsharp I have the need to provide a javascript File.File(). The file I want to give it is locally saved and could be anything from pdfs to office documents or images. As far as CefSharp is concerned I have implemented a ISchemeHandlerFactory and ResourceHandler adding a test:// scheme and for example I have successfully added a JS file like this.
var head = document.head;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'test://local/folder/mytest.js';
head.appendChild(script);
根据API创建我需要的文件
According to the API to create a file I need
位-ArrayBuffer,ArrayBufferView,Blob或DOMString对象的数组-或任何此类对象的混合。这是编码为UTF-8的文件内容。
bits - An Array of ArrayBuffer, ArrayBufferView, Blob, or DOMString objects — or a mix of any such objects. This is the file content encoded as UTF-8.
所以我有test://的方案,可以给我一个本地文件我需要在javascript中使用什么才能将其保存到文件中?
So I have my scheme of test:// to give me a local file what do I need to use in javascript to get this into a file?
推荐答案
我工作了。我首先尝试了访存,但是这不允许我使用自定义方案。所以我不得不使用XMLHttpRequest
I worked it. I first tried a fetch but that would not let me use a custom scheme. So I had to use XMLHttpRequest
(function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var contentType = xhttp.getResponseHeader("Content-Type");
var file = new File([this.response],'3Markets.xlsx', {type: contentType,});
//use my new file etc
}
};
xhttp.open('GET', 'test://local/folder/3Markets.xlsx', true);
xhttp.responseType = "arraybuffer";
xhttp.send();
})()
我唯一的问题或担心是我目前必须致电
The only issue or worry i have is I currently have to call
CefCommandLineArgs.Add("disable-web-security", "disable-web-security")
我将不得不看看如何在不禁用网络安全的情况下实现此目标,或者最终询问在这里提问。
which i will have to have a look around how I can achieve this without disabling web security or eventually ask a question here.
这篇关于使用CefSharp和SchemeHandler如何创建Javascript File()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!