2011-11-02 24 views
0

我正在使用歌劇小部件在每個頁面上提供到我的網站的鏈接。我怎樣才能加載外部來源的數據部件內的數據?如何從歌劇小部件中的文件導入數據

我想在桌面的opera小部件中顯示來自sql數據庫的數據。有沒有辦法做到這一點?是否可以從小部件中的文件獲取數據?

請指引我...

回答

2

這不是可以從一個小部件訪問外部SQL數據庫,但它可以讀取從一個小部件本地文本文件。以下簡單示例適用於安裝了最新Opera的桌面(11.52)。

config.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<widget xmlns="http://www.w3.org/ns/widgets" id="http://example.com/filereadertest" defaultlocale="en" width="400" height="500"> 
    <name>File Reader Test</name> 
    <description>File Reader test widget</description> 
    <author href="http://example.com/author/">Me</author> 
    <feature name="http://xmlns.opera.com/fileio"> 
     <param name="folderhint" value="home" /> 
    </feature> 
</widget> 

的index.html:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<style> 
body { 
    background:#eee; 
} 
#output { 
    width:98%; 
    height:200px; 
} 
</style> 
</head> 
<body> 
<div> 
    <p>Get the contents of a text file</p> 
    <p><textarea id="output"></textarea></p> 
    <p><button id="open">Open</button></p> 
</div> 
<script> 
window.addEventListener('DOMContentLoaded', function() { 

    function openFile(evt) { 
     opera.io.filesystem.browseForFile("test", "", openFileCallback) 
    } 

    function openFileCallback(file) { 
     if (file) { 
      fstream = file.open(file, "r"); 
      var output = document.getElementById("output"); 
      output.value = ""; 
      while (!fstream.eof) { 
       output.value += fstream.readLine("UTF-8"); 
      } 
     } 
    } 

    document.getElementById("open").addEventListener("click", openFile, false); 

}, false); 
</script> 
</body> 
</html> 

更多的文檔是在這裏: Widget File I/O API