2016-09-16 47 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,如果我的回答对你有帮助,你能不能标记为已接受? –