Angular 2 - 如何在打字稿中使用 FileReader 从给定的 URL 中读取文件?
Angular 2 - How to read the file from the given URL using FileReader in typescript?
我有 jenkins build,其中包含 Build Artifacts。工件包含一个文本文件。我想访问这个文件并读取文件。此文件包含某些版本 tag.I 想要在 Angular 2 typescript 应用程序中显示此文件内容。
Jenkins REST API to access the artifact file -
http://localhost:8080/job/myjobname/job/develop/59/artifact/build/docker/myFileName.txt
只要我在浏览器中点击 URL,它就会下载文本文件。
我只想在我的 Angular 2 typescript 应用程序中读取这个给定 URL 的文件。我如何使用 FileReader 从 URL 读取此文件?还有其他更好的方法吗?
FileReader
只能读取 Blob(Blob
and the subclassed File
object). To read an URL you can use either the new fetch()
or the classic XMLHttpRequest()
。您当然可以将内容请求为 blob,然后通过 FileReader
读取它,但如果内容是文本,则直接阅读它更有意义。
通常 CORS restriction 你也必须处理,除非 URL 指向与你的调用页面相同来源的某个地方。
我有 jenkins build,其中包含 Build Artifacts。工件包含一个文本文件。我想访问这个文件并读取文件。此文件包含某些版本 tag.I 想要在 Angular 2 typescript 应用程序中显示此文件内容。
Jenkins REST API to access the artifact file - http://localhost:8080/job/myjobname/job/develop/59/artifact/build/docker/myFileName.txt
只要我在浏览器中点击 URL,它就会下载文本文件。
我只想在我的 Angular 2 typescript 应用程序中读取这个给定 URL 的文件。我如何使用 FileReader 从 URL 读取此文件?还有其他更好的方法吗?
FileReader
只能读取 Blob(Blob
and the subclassed File
object). To read an URL you can use either the new fetch()
or the classic XMLHttpRequest()
。您当然可以将内容请求为 blob,然后通过 FileReader
读取它,但如果内容是文本,则直接阅读它更有意义。
通常 CORS restriction 你也必须处理,除非 URL 指向与你的调用页面相同来源的某个地方。