我无法使用 CloudBlockBlob 将照片上传到 Azure 存储
//module level variables
CameraCaptureTask cameraCaptureTask;
Stream _media;
// Constructor
public MainPage()
{
InitializeComponent();
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
cameraCaptureTask.Show();
}
private async void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
_media = e.ChosenPhoto;
ShowPhotoOnScreen();
var todoItem = CreateToDo(e);
await UploadFileToAzureStorage(todoItem);
}
//Works good till here. It will go off and never come back.
private async Task UploadFileToAzureStorage(TodoItem todoItem)
{
Stream _media ;
// Upload the new image as a BLOB from the stream.
CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(todoItem.ResourceName);
_photoCache.Position = 0;
await blobFromSASCredential.UploadFromStreamAsync(_media );
}
这将异步启动并且永远不会返回。我有一种感觉,我在流中做错了什么。
我的问题:
- 我应该如何处理流?
- 我在 UploadFromStreamAsync 上没有看到已完成的事件,是否有一种巧妙的方法来捕获 OnCompleted 或我应该使用的其他方法?
在 Azure 存储教程中可以找到类似的代码,并且适用于 Win8: http: //www.windowsazure.com/en-us/develop/mobile/tutorials/upload-images-to-storage-dotnet/
StorageFile _media = null;
// Get the new image as a stream.
using (var fileStream = await _media.OpenStreamForReadAsync())
{
...
// Upload the new image as a BLOB from the stream.
CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(todoItem.ResourceName);
await blobFromSASCredential.UploadFromStreamAsync(fileStream.AsInputStream());
}
Win8 与 WP8 示例的主要区别是
- 照片是在 WP8 中捕获的 StorageFile (Win8) 和 Stream。
- AsInputStream 在 WP8 中似乎无效
如果您需要更多信息,请告诉我。谢谢。