2016-09-16 188 views
0

我正在編寫一個簡單的UWP應用程序,其中用戶將InkCanvas筆觸信息發送到Azure blockBlob,然後檢索一些不同的InkCanvas筆觸容器信息以呈現到畫布。UWP CloudBlob.DownloadFileAsync訪問被拒絕錯誤

使用StrokeContainer.saveAsync()將.ink文件保存到applicationData本地文件夾到相同的位置和文件名(它將被每個事務替換),然後使用CloudBlockBlob.uploadAsync()將其上傳。

我嘗試從Azure服務器下載文件時出現問題 - 出現「拒絕訪問」錯誤。

async private void loadInkCanvas(string name) 
    { 
     //load ink canvas 
     //add strokes to the end of the name 
     storageInfo.blockBlob = storageInfo.container.GetBlockBlobReference(name + "_strokes"); 
     //check to see if the strokes file exists 
     if (await storageInfo.blockBlob.ExistsAsync){ 
      //then the stroke exists, we can load it in. 
      StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 
      StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting); 
      using (var outputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) 
      { 
       await storageInfo.blockBlob.DownloadToFileAsync(storageFile);//gives me the "Access Denied" error here 

      } 
     } 
    } 

任何幫助將不勝感激,所有我在網上找到的,你不應該把直接路徑到目標位置,而要用ApplicationData.Current.LocalFolder。

+1

請看看這裏:http://www.damirscorner.com/blog/posts/20120419-ClosingStreamsInWinRt.html。 HTH。 –

回答

0

DownloadToFileAsync方法確實可以幫助您將文件流從Azure存儲讀取到本地文件,您無需打開本地文件供您自己閱讀。在你的代碼中,你打開文件流並佔用文件,然後調用DownloadToFileAsync方法試圖訪問導致「拒絕訪問」異常的佔用文件。

解決辦法很簡單,只要下載到本地文件沒有打開,代碼如下:

if (await blockBlob.ExistsAsync()) 
{ 
    //then the stroke exists, we can load it in. 
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 
    StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting); 
    await blockBlob.DownloadToFileAsync(storageFile);  
} 

如果你想自己來讀取文件流,你需要使用DownloadToStreamAsync方法而不是DownloadToFileAsync如下所示:

if (await blockBlob.ExistsAsync()) 
{  
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 
    StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting); 
    //await blockBlob.DownloadToFileAsync(storageFile); 
    using (var outputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) 
    { 
     await blockBlob.DownloadToStreamAsync(outputStream.AsStreamForWrite());  
    } 
} 
+0

謝謝!是的,我發現那是我的問題,我必須毫無意識地在那裏寫下它,並且錯過了最明顯的解決方案。 –

+0

@ConnorBoutin,如果我的回答對你有幫助,你能不能標記爲已接受? –