【问题标题】:How can I get text from txt URL ? How can I use Filereader from Angular 7?如何从 txt URL 获取文本?如何使用 Angular 7 中的 Filereader?
【发布时间】:2019-07-08 06:20:28
【问题描述】:

“我想从 txt url 中获取文本数据,并在 angular web 中获取它。如何从 txt URL 获取文本?如何使用 Angular 7 中的 Filereader ???”

“这是针对 Angular 7 的。”

<a (click)="getTextFile()">Check</a> 
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {
  HttpClient,
  HttpRequest,
  HttpResponse,
  HttpParams,
  HttpHeaders
} from '@angular/common/http';




@Component({
  selector: 'autocomplete-overview-example',
  templateUrl: 'autocomplete-overview-example.html',
  styleUrls: ['autocomplete-overview-example.css'],
})
export class AutocompleteOverviewExample {
 text:any;

  constructor(
     private http: HttpClient,
  ) {

  }


  getTextFile():void{
  this.http.get('https://creamson-intelli.slack.com/files/UFL3QQFP0/FL4N4SNFJ/young_ruskin_bond.txt').subscribe((data) => {
    this.text=data;
    console.log(this.text);
})
}
} 

【问题讨论】:

标签: angular


【解决方案1】:

检查一下

Working Example

service.ts

  getRawData(): Observable<any> {
    const api = 'https://www.w3.org/TR/PNG/iso_8859-1.txt';
    return this.httpClient.get(api,{ responseType: 'text' });    
  }

组件.ts

import { Component, OnInit } from '@angular/core';

import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  name = 'Angular 5';
  rawlist: any;

  constructor(private appService: AppService) {}

  ngOnInit() {}

  getRawData() {
    this.appService
      .getRawData()
      .subscribe(
        data => this.rawlist=data,
        error => console.log(error)
      );
  }

}

.html

<button (click)="getRawData()">get raw data </button>

 {{rawlist }}

【讨论】:

  • 我不需要本地服务器的任何文件上传器。是否可以从服务器 url txt 文件中处理它。??
  • HttpErrorResponse {headers: {...}, status: 0, statusText: "Unknown Error", url: "creamson-intelli.slack.com/files/UFL3QQFP0/FL4N4SNFJ/…"...} 错误:ProgressEvent headers: HttpHeaders message: "Http failure response for @ 987654323@:0 未知错误”名称:“HttpErrorResponse”ok:错误状态:0 statusText:“未知错误”url:“creamson-intelli.slack.com/files/UFL3QQFP0/FL4N4SNFJ/…proto:HttpErrorResponse
  • 错误是我重新定位 txt 文件检查 url 的语法
  • 如果 url 来自 something.com/something/example.txt 则错误为错误:ProgressEvent isTrusted: true proto: ProgressEvent
  • @souvicksaha 你能把你首先要阅读的示例文本发给你吗
猜你喜欢
  • 2019-07-28
  • 2013-05-16
  • 1970-01-01
  • 2019-05-08
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 2014-08-06
相关资源
最近更新 更多