2013-05-29 77 views
0

雖然在Windows 8應用的工作,我發現我可以打開並使用像得到一個文件對象的引用:如何使用Windows.Storage FileOpenPicker類獲取標準JavaScript File API對象?

// Markup 
<input type='file' id='myfile'/> 

// JavaScript 
var fInput = document.getElementById('myFile'); 
fInput.onchange = function (e) { 
    var dataSource = e.target; 
    var file = dataSource.files[0]; // object of type 'File' 
} 

不過,我想用一個文件選擇器來呈現用戶無需按下「瀏覽」按鈕。所以,我嘗試使用FilePicker類是這樣的:

var picker = new Windows.Storage.Pickers.FileOpenPicker(); 
picker.pickSingleFileAsync().then(function (file) { 
    // in this case, file is a 'StorageFile' object 
}); 

所以現在的問題是,可以pickSingleFileAsync莫名其妙地返回File對象,而不是StorageFile

回答

1

使用MSApp.createFileFromStorageFile。忽略此處的文檔錯誤。它講述了api的功能。

// TODO - more code is required here to initialize file open picker. 
// refer the file picker sample 
var picker = new Windows.Storage.Pickers.FileOpenPicker(); 
picker.pickSingleFileAsync().then(function (storageFile) { 
    // ignore when file pick is cancelled 
    if (!storageFile) 
     return; 
    var file = MSApp.createFileFromStorageFile(storageFile); 
}); 
+0

究竟是我在找什麼,謝謝! – lostsource