簡體   English   中英

如何從本地存儲在javascript中的文件中讀取內容?

[英]How to read content from a file stored locally in javascript?

Windows 8應用程序開發需要文件訪問權限,而我正在將html與javascript一起使用。 我閱讀了msdn文章以及此處發布的其他問題。 但是每個答案都指向訪問通過html文件中的用戶輸入讀取的文件。 但是我想使用java FileIO來訪問文件內容。 我的操作將需要從文件的第一行讀入一個變量...請幫助我。

我認為發布在類似問題中的鏈接(在這里https://stackoverflow.com/a/13131746/173100 )會有所幫助。 請注意,您無法從文件系統中讀取任意文件,允許的位置包括應用程序的安裝位置,應用程序的本地數據文件夾以及鏈接的MSDN文章( http://msdn.microsoft。 com / zh-CN / library / windows / apps / hh967755.aspx )。 您最有可能對http://msdn.microsoft.com/zh-cn/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
   }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM