如何从导入的库读取文件 [英] How to read file from an imported library

查看:95
本文介绍了如何从导入的库读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包: webserver utils ,它向web服务器提供资产。



webserver 需要访问utils中的静态文件。所以我有这个设置:

  utils / 
lib /
static.html

如何访问 static.html dart scripts in webserver



EDIT :到目前为止,使用镜像来获取库的路径,并从那里读取它。该方法的问题是,如果utils包含在 package:中, url currentMirrorSystem()。findLibrary(#utils).uri 是一个包uri,不能转换为实际的文件实体。



使用示例:

$ b $

  var resource = new Resource('package:myapp / myfile.txt'); 
var contents = await resource.loadAsString();
print(contents);

这是在虚拟机上工作的,从1.12开始。



然而,这并不直接解决你从包:URI到达实际File实体的需要。现在给定Resource类,你必须将 loadAsString()的字节路由到HTTP服务器的Response对象中。


I have two packages: webserver and utils which provides assets to webserver.

The webserver needs access to static files inside utils. So I have this setup:

utils/
  lib/
    static.html

How can I access the static.html file in one of my dart scripts in webserver?

EDIT: What I tried so far, is to use mirrors to get the path of the library, and read it from there. The problem with that approach is, that if utils is included with package:, the url returned by currentMirrorSystem().findLibrary(#utils).uri is a package uri, that can't be transformed to an actual file entity.

解决方案

Use the Resource class, a new class in Dart SDK 1.12.

Usage example:

var resource = new Resource('package:myapp/myfile.txt');
var contents = await resource.loadAsString();
print(contents);

This works on the VM, as of 1.12.

However, this doesn't directly address your need to get to the actual File entity, from a package: URI. Given the Resource class today, you'd have to route the bytes from loadAsString() into the HTTP server's Response object.

这篇关于如何从导入的库读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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