【问题标题】:Show pdf and docx in html using angularjs and AWS S3使用 angularjs 和 AWS S3 在 html 中显示 pdf 和 docx
【发布时间】:2018-03-08 18:13:06
【问题描述】:

我正在使用 AngularJS 和 AWS S3,我正在尝试在浏览器中查看文件(.pdf、.docx、.ppt 等),但我卡住了。

我正在尝试使用 google 文档视图:

<iframe src="http://docs.google.com/gview?url=MY_AWS_S3_URL&embedded=true"></iframe>

但是我在控制台的浏览器中收到this 错误,显然这个文档的视图不起作用。

有人知道发生了什么吗?可能是 S3 权限错误,或者可能是因为 angularjs 不支持它?

编辑: 我试过这个来显示 .pdf 文件,它可以工作,但只适用于 .pdf 文件。

<object width="400" height="500" type="application/pdf" data="your_url" id="pdf_content">
  <p>ERROR!!</p>
</object>

【问题讨论】:

  • 是否需要 url-escape (percent-encode) MY_AWS_S3_URL
  • @Michael-sqlbot 我没看懂答案
  • 第一个? 之后的URL 部分称为查询字符串。您将一个 URL (S3) 嵌入到另一个 URL (google docs) 中,这通常意味着必须使用 URL-escaping/percent-encoding 对内部 URL 中的某些字符进行转义,以避免出现歧义,例如使用类似EncodeURIComponent 的东西。您在构建 Google Docs URL 时是否对 S3 URL 执行此操作?如果没有,可能需要它。
  • @Michael-sqlbot 太好了,现在我可以在浏览器中查看它,但它仍然隐藏在 html 中,显示与我发布的相同错误,现在它看起来更多来自 AngularJS因为这个:“错误:$sce:insecurl 对来自不受信任源的资源的处理被阻止”

标签: html angularjs amazon-web-services pdf amazon-s3


【解决方案1】:

首先,感谢@Michael-sqlbot 告诉我EncodeURIComponent

第二步是在this 线程之后可信地为AngularJS 设置URL。

事实是,我必须在我的 S3 URL 上做这个:

encodeURIComponent(my.s3_url);

然后我不得不向我的控制器调用 $sce 库(就像我解释的那样,我们必须使 AngularJS 的 URL 可信),如下所示:

JS

.module('module.name')
    .controller('exExExample', [
        '$scope',
        '$location',
        '$sce',
        'info',
    function($scope, $location, $sce, info) {
    this.my.s3_url = info.url;

    // Here I encode my AWS S3 url
    this.my.complete_url ='http://docs.google.com/gview?url=' + encodeURIComponent(my.s3_url) + '&embedded=true';
    //Using a function of the library $sce called trustAsResourceUrl()
    this.my.complete_url = $sce.trustAsResourceUrl(this.complete_url);
  }]);

HTML

<iframe ng-src="{{exemple.my.complete_url}}" ></iframe>

而且效果很好,所以我希望这个解释能帮助像它帮助我的人。

【讨论】:

  • 很高兴您解决了问题。请考虑在帮助中心查看How Do I Write a Good Answer?。理想情况下,您应该在实际代码中包含您更改的内容,并附上解释——直接有用的东西,而不是简单地引用其他地方。由于缺乏实质内容,此答案可能会被视为否决或删除的候选者。
  • @Michael-sqlbot Okey,感谢您提供的信息,我已经完成了。希望将来可以帮助某人。
猜你喜欢
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多