Using CefSharp and a SchemeHandler how to create a Javascript File()

大憨熊 提交于 2019-12-13 04:21:57

问题


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);

According to the API to create a file I need

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/54391137/using-cefsharp-and-a-schemehandler-how-to-create-a-javascript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!