相关疑难解决方法(0)

如何使用javascript将图像转换为base64字符串

我需要将我的图像转换为base64字符串,以便我可以将我的图像发送到服务器.这个有没有js文件......?否则如何转换它

javascript base64

456
推荐指数
12
解决办法
63万
查看次数

在JavaScript中,我如何/应该使用XMLHttpRequest的async/await?

完全披露:我有资格拥有中级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)

javascript xmlhttprequest async-await

22
推荐指数
3
解决办法
2万
查看次数

PDF 不显示图像:html2pdf 库

我使用 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

请帮助,问候。

html2pdf html2canvas jspdf typescript angular

4
推荐指数
1
解决办法
7572
查看次数