2013-10-29 2 views
2

메인()에서 url에서 파일을 읽고 싶습니다. 그것은 HttpRequest from "dart:html"를 사용하는 정말 쉽습니다 솔기하지만 명령 줄에서 작동하지 않습니다 :다트로 메인에있는 URL에서 데이터 컨텐츠를 읽으십시오.

import "dart:html" ; 

main(){ 
    String url ="http://foo.bar/foo.txt"; 
    HttpRequest.getString(url).then((content){ 
    print(content); 
    }); 
} 

=> 내장 된 라이브러리 '다트 : HTML은'독립 실행 형 VM에서 사용할 수 없습니다.

어떻게하면됩니까?

답변

3

인쇄 내용 :

import 'dart:convert'; 
import 'dart:io'; 

main() { 
    new HttpClient().getUrl(Uri.parse('http://foo.bar/foo.txt')) 
    .then((HttpClientRequest request) => request.close()) 
    .then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(print)); 
} 

또는 파일에 쓰기 :

import 'dart:convert'; 
import 'dart:io'; 

main() { 
    new HttpClient().getUrl(Uri.parse('http://foo.bar/foo.txt')) 
    .then((HttpClientRequest request) => request.close()) 
    .then((HttpClientResponse response) => response.pipe(new File('foo.txt').openWrite())); 
} 
관련 문제