我需要将我的图像转换为base64字符串,以便我可以将我的图像发送到服务器.这个有没有js文件......?否则如何转换它
完全披露:我有资格拥有中级JavaScript知识.所以这略高于我此时的经验水平.
我有一个Google Chrome扩展程序,只要file:///页面加载就会为本地执行AJAX请求.在我从请求中得到响应之后,我将在代码中使用多个函数中返回的代码.大部分时间我都会在需要运行的代码之前收到响应.但有时我不会,一切都会破裂.
现在,我假设我可以抛出下面的所有相关代码xhr.onload.但这似乎效率低下?我有许多依赖于响应的活动部件,将它们全部放在那里似乎很糟糕.
我已经阅读了几篇与async/await相关的文章,并且我在理解这个概念时遇到了麻烦.我也不是100%肯定我正在以正确的方式看待这个问题.我是否应该考虑使用async/await?
这是我的AJAX请求的代码.
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e) {
code = xhr.response;
};
xhr.onerror = function () {
console.error("** An error occurred during the XMLHttpRequest");
};
xhr.send();
Run Code Online (Sandbox Code Playgroud)
假设我后来在我的代码中有一些需要触发的函数.现在他们看起来像:
function doTheThing(code) {
// I hope the response is ready.
}
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法来解决这个问题?仅供参考,FetchAPI不是一种选择.
这是我的代码结构的高级视图.
// AJAX request begins.
// ...
// A whole bunch of synchronous code that isn't dependant on
// the results of my AJAX request. …Run Code Online (Sandbox Code Playgroud) 我使用 Angular 5 和 html2pdf 库来帮助创建 pdf 文件。 https://github.com/eKoopmans/html2pdf
这用于我的 Angular 方法。
var element = document.getElementById('document');
var opt = {
margin: [10, 0, 10, 0],
filename: `document.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true },
jsPDF: { unit: 'mm', format: 'letter', orientation: 'portrait' }
};
html2pdf().from(element).set(opt).save();
Run Code Online (Sandbox Code Playgroud)
导出 pdf 后,我无法从网上看到任何图像,但它显示了本地存储的图像。这是在线图像的示例。 https://s3.amazonaws.com/images.anterasoftware.com/5b6c5886622d5Screen_Shot_2018-06-27_at_10.37.03_AM.png
请帮助,问候。
javascript ×2
angular ×1
async-await ×1
base64 ×1
html2canvas ×1
html2pdf ×1
jspdf ×1
typescript ×1