Javascript,Typescript,Angular 5-打开和读取文件 [英] Javascript, Typescript, Angular 5 - Open and read file

查看:72
本文介绍了Javascript,Typescript,Angular 5-打开和读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 5,我有一个应用程序,需要在其中读取文本的AMP HTML文件.该文件包含在一个组件中,并且只能从该组件访问.

I am working with Angular 5, I have an application in which I need to read an AMP HTML file as text. This file is contained in a component and should only be accessed from this component.

我希望能够通过给出文件名来以只读方式打开文件.

I would like to be able to open the file in read-only by giving its name.

我实际上正在搜索如下内容:

I'm actually searching for something like this:

let file = open('amp.html');

有可能吗?如果没有,我该怎么做?

Is it possible? If not how can I do to achieve this?

推荐答案

如果您正在编写浏览器端JS

您不能只是简单地读取文件.JS正在您的浏览器上运行,您需要考虑从何处获取该文件.

If you're writing browserside JS

You can't just simply read a file. The JS is running on your browser, and you need to think about where you're getting that file from.

如果文件在服务器上,则需要从该文件中获取首先通过请求服务器.

If the file is on a server, you need to fetch that file from the server first by making a request for it.

如果您正在读取用户计算机上的文件,则将使用文件API 在浏览器上,以允许用户选择该文件.

If you're reading a file on the user's computer, you're gonna be using the File API on the browser to allow the user to select that file.

假设您正在使用NodeJS,则可以像使用其他编程语言一样进行文件操作.检出 fs 模块

Assuming you're using NodeJS, you can conduct file operations like you would with other programming languages. Check out the fs module

这篇关于Javascript,Typescript,Angular 5-打开和读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆