如何从javascript本地存储的文件中读取内容?

时间:2013-12-27 18:10:44

标签: javascript file-io windows-8

Windows 8应用开发需要文件访问权限,我正在使用带有javascript的html。我阅读了msdn文章以及此处发布的其他问题。但每个答案都指向通过html文件中的用户输入访问文件。但我想访问文件内容,如使用java FileIO。 我的操作是要求从文件读取变量的第一行......请帮助我。

1 个答案:

答案 0 :(得分:0)

我认为在类似问题(此处https://stackoverflow.com/a/13131746/173100)中发布的链接会有所帮助。请注意,您无法从文件系统中读取任意文件,允许的位置包括应用程序的安装位置,应用程序的本地数据文件夹以及链接的MSDN文章(http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx)中描述的一堆预定义文件夹。您很可能对http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.getfilefromapplicationuriasync.aspx感兴趣。

实际上,您可以阅读位于应用安装目录(=在应用包中)的文件foo.txt的第一行,如下所示:

Windows.Storage.StorageFile.getFileFromApplicationUriAsync("ms-appx:///foo.txt").done(
   function(file) {
       // Assuming file has content here, better error handling probably needed
       var lines = file.split("\n");
       var firstLine = lines[0];

       // Do something with the firstLine variable
   },
   function(error) {
       // Error occurred, handle it here
   }
);
相关问题