簡體   English   中英

如何從SkyDrive下載文件到IsolatedStorage?

[英]How to download a file from SkyDrive into IsolatedStorage?

我正在編寫一個WP8(XAML和C#)應用程序來從SkyDrive讀取文本文件並將其保存到IsolatedStorage中。

似乎讀取了文件,但我得到的數據是文件的描述而不是文件的內容。

SkyDrive中的文件名是:“myFile1.txt”
該文件的內容是:“這是一個測試文件1”

我的代碼如下:

private async void btnDownload_Click( object sender, System.Windows.RoutedEventArgs e )
    {
    string fileID = "file.17ff6330f5f26b89.17FF6330F5F26B89!1644";
    string filename = "myDownloadFile1.txt";
    var liveClient = new LiveConnectClient( LiveHelper.Session );

    // Download the file
    liveClient.BackgroundDownloadAsync( fileID, new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );  

    // Read the file
    var FileData = await LoadDataFile< string >( filename );
    Debug.WriteLine( "FileData: " + (string)FileData );
    }


public async Task<string> LoadDataFile<T>( string fileName )
    {
    // Get a reference to the Local Folder
    string root = ApplicationData.Current.LocalFolder.Path;
    var storageFolder = await StorageFolder.GetFolderFromPathAsync( root + @"\shared\transfers" ); 

    bool IsFileExist = await StorageHelper.FileExistsAsync( fileName, storageFolder );
    if ( IsFileExist == true )
        {
        StorageFile storageFile = await storageFolder.GetFileAsync( fileName );
        if ( storageFile != null )
            {
            // Open it and read the contents
            Stream readStream = await storageFile.OpenStreamForReadAsync();
            using ( StreamReader reader = new StreamReader( readStream ) )
                {
                string _String = await reader.ReadToEndAsync();
                reader.Close();
                return _String;
                }
            }
        else
            {
            return string.Empty;
            }
        } 
    return string.Empty;
    }
}

我運行此代碼的數據是:

{
   "id": "file.17ff6330f5f26b89.17FF6330F5F26B89!1644", 
   "from": {
      "name": "Eitan Barazani", 
      "id": "17ff6330f5f26b89"
   }, 
   "name": "myFile1.txt", 
   "description": "", 
   "parent_id": "folder.17ff6330f5f26b89.17FF6330F5F26B89!1643", 
   "size": 23, 
   "upload_location": "https://apis.live.net/v5.0/file.17ff6330f5f26b89.17FF6330F5F26B89!1644/content/", 
   "comments_count": 0, 
   "comments_enabled": false, 
   "is_embeddable": true, 
   "source": "https://fculgq.bay.livefilestore.com/y2m0zkxi9kpb4orfYNSLSwst5Wy3Z7g6wDj7CM3B6wcOth9eA-gUflXeSCAAH_JWx2co72sgOTcGgvkwQGI3Gn5E1qXnRoKpVbsX_olRrB5gnCNIm8GrUrORco8_-je1cet/myFile1.txt?psid=1", 
   "link": "https://skydrive.live.com/redir.aspx?cid=17ff6330f5f26b89&page=view&resid=17FF6330F5F26B89!1644&parid=17FF6330F5F26B89!1643", 
   "type": "file", 
   "shared_with": {
      "access": "Just me"
   }, 
   "created_time": "2013-07-15T16:29:10+0000", 
   "updated_time": "2013-07-15T16:29:10+0000"
}

我不知道為什么我沒有獲取文件中的數據。 知道我做錯了什么嗎?

謝謝,

我想你需要:

liveClient.BackgroundDownloadAsync( fileID+"/content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );

即在fileID字符串之后,您需要添加"/content"以獲取文件內容,而不是元數據。

您在元數據中獲得了一個鏈接:

"source": "https://fculgq.bay.livefilestore.com/y2m0zkxi9kpb4orfYNSLSwst5Wy3Z7g6wDj7CM3B6wcOth9eA-gUflXeSCAAH_JWx2co72sgOTcGgvkwQGI3Gn5E1qXnRoKpVbsX_olRrB5gnCNIm8GrUrORco8_-je1cet/myFile1.txt?psid=1"

使用它來下載文件,例如HttpWebRequest

這是來自MSDN鏈接,它解釋了Source參數的含義 - 例如,音頻文件

我的代碼有兩處變化:

    private async void btnDownload_Click( object sender, System.Windows.RoutedEventArgs e )
        {
        string fileID = "file.17ff6230f5f26b89.17FF6230F5F26B89!1658";
        string filename = "myDownloadFile1.txt";
        var liveClient = new LiveConnectClient( LiveHelper.Session );

        // Download the file
        await liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );

        // Read the file
        var FileData = await LoadDataFile<string>( filename );
        Debug.WriteLine( "FileData: " + (string)FileData );
        }


    public async Task<string> LoadDataFile<T>( string fileName )
        {
        // Get a reference to the Local Folder
        string root = ApplicationData.Current.LocalFolder.Path;
        var storageFolder = await StorageFolder.GetFolderFromPathAsync( root + @"\shared\transfers" );

        bool isFileExist = await StorageHelper.FileExistsAsync( fileName, storageFolder );
        if ( isFileExist )
            {
            // Create the file in the local folder, or if it already exists, just replace
            StorageFile storageFile = await storageFolder.GetFileAsync( fileName );

            if ( storageFile != null )
                {
                // Open it and read the contents
                Stream readStream = await storageFile.OpenStreamForReadAsync();
                using ( StreamReader reader = new StreamReader( readStream ) )
                    {
                    string _String = await reader.ReadToEndAsync();
                    reader.Close();
                    return _String;
                    }
                }
            return string.Empty;
            }
        return string.Empty;
        }
    }

上面的代碼正確讀取文件。 我需要在fileID中添加“/ content”。 此外,await將無法在模擬器中工作,但在設備中工作正常? (這對我來說很新......)。

暫無
暫無

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

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