public async Task UploadPicture(UploadViewModel uploadViewModel) { var sasUrl = await this.mobileService.GenerateStorageAccessSignatureAsync(uploadViewModel.ImageIdentifier); var request = HttpWebRequest.Create(sasUrl) as HttpWebRequest; request.Method = "PUT"; uploadViewModel.PictureStream.Position = 0; #if ANDROID request.ContentLength = uploadViewModel.PictureStream.Length; #else request.Headers["Content-Length"] = uploadViewModel.PictureStream.Length.ToString(); #endif request.Headers["x-ms-blob-type"] = "BlockBlob"; var pictureData = new byte[uploadViewModel.PictureStream.Length]; await uploadViewModel.PictureStream.ReadAsync(pictureData, 0, (int)uploadViewModel.PictureStream.Length); using (var requestStream = await Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null)) { await requestStream.WriteAsync(pictureData, 0, pictureData.Length); } var response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null); }
public async Task<string> UploadImage(Bitmap bitmap) { var uploadVM = new UploadViewModel(); var stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); uploadVM.PictureStream = stream; uploadVM.ImageIdentifier = Guid.NewGuid() + ".bmp"; await azureStorageService.UploadPicture(uploadVM); return string.Format("{0}{1}", AndroidPollImageViewModel.PollPicturesUrlBase, uploadVM.ImageIdentifier); }
public async Task<UploadViewModel> ShowChooser() { UploadViewModel uploadViewModel = null; #if ANDROID var uiContext = new Services.UiContext(); var mediaPicker = new Xamarin.Media.MediaPicker(uiContext.CurrentContext); #elif IOS var mediaPicker = new Xamarin.Media.MediaPicker(); #endif #if __MOBILE__ try { var mediaFile = await mediaPicker.PickPhotoAsync().ConfigureAwait(false); if (mediaFile != null) { uploadViewModel = new UploadViewModel(); uploadViewModel.PictureStream = mediaFile.GetStream(); uploadViewModel.ImageIdentifier = Guid.NewGuid() + Path.GetExtension(mediaFile.Path); } } catch (TaskCanceledException ex) { // Happens if they don't select an image and press the // back button. Just continue as it is an expected // exception. Insights.Report(ex); } #else var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); openPicker.FileTypeFilter.Add(".bmp"); var file = await openPicker.PickSingleFileAsync(); if (file != null) { uploadViewModel = new UploadViewModel(); uploadViewModel.PictureFile = file; uploadViewModel.PictureStream = await file.OpenReadAsync(); uploadViewModel.ImageIdentifier = Guid.NewGuid() + file.FileType; } #endif return uploadViewModel; }
public async Task UploadPicture(UploadViewModel uploadViewModel) { var sasUri = new Uri(await this.mobileService.GenerateStorageAccessSignatureAsync(uploadViewModel.ImageIdentifier)); var sasKey = sasUri.Query.Substring(1); var blobService = new CloudBlobClient( new Uri(string.Format("{0}://{1}", sasUri.Scheme, sasUri.Host), UriKind.Absolute), new StorageCredentials(sasKey)); var container = blobService.GetContainerReference(Container); var blob = container.GetBlockBlobReference(uploadViewModel.ImageIdentifier); using (var fileStream = await uploadViewModel.PictureFile.OpenAsync(FileAccessMode.Read)) { await blob.UploadFromStreamAsync(fileStream); } }
public async Task<UploadViewModel> ShowChooser() { UploadViewModel uploadViewModel = null; var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); openPicker.FileTypeFilter.Add(".bmp"); var file = await openPicker.PickSingleFileAsync(); if (file != null) { uploadViewModel = new UploadViewModel(); uploadViewModel.PictureFile = file; uploadViewModel.PictureStream = await file.OpenReadAsync(); uploadViewModel.ImageIdentifier = Guid.NewGuid() + file.FileType; } return uploadViewModel; }
public async Task UploadPicture(UploadViewModel uploadViewModel) { var sasUrl = await this.mobileService.GenerateStorageAccessSignatureAsync().ConfigureAwait(false); #if NETFX_CORE var blobService = new CloudBlobClient( new Uri(sasUrl.Replace("\"", string.Empty), UriKind.Absolute)); var container = blobService.GetContainerReference(Container); var blob = container.GetBlockBlobReference(uploadViewModel.ImageIdentifier); using (var fileStream = await uploadViewModel.PictureFile.OpenAsync(FileAccessMode.Read)) { await blob.UploadFromStreamAsync(fileStream); } #else // strangely it expects /pollimages/pollimages sasUrl = sasUrl.Replace("/pollimages", String.Format("/pollimages/pollimages/{0}", uploadViewModel.ImageIdentifier)); var request = (HttpWebRequest)WebRequest.Create(sasUrl); request.Method = "PUT"; uploadViewModel.PictureStream.Position = 0; request.ContentLength = uploadViewModel.PictureStream.Length; request.Headers["x-ms-blob-type"] = "BlockBlob"; request.Headers["x-ms-blob-content-disposition"] = String.Format("attachment; filename=\"pollimages/{0}\"", uploadViewModel.ImageIdentifier); var pictureData = new byte[uploadViewModel.PictureStream.Length]; await uploadViewModel.PictureStream.ReadAsync(pictureData, 0, (int)uploadViewModel.PictureStream.Length); using (var requestStream = await Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null).ConfigureAwait(false)) { await requestStream.WriteAsync(pictureData, 0, pictureData.Length).ConfigureAwait(false); } var response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null).ConfigureAwait(false); #endif // NETFX_CORE }
public async Task<UploadViewModel> ShowChooser() { return await Task.Run(() => { var task = new TaskCompletionSource<UploadViewModel>(); EventHandler<PhotoResult> completedHandler = null; completedHandler = (sender, e) => { this.photoChooserTask.Completed -= completedHandler; if (e.Error != null) { task.SetException(e.Error); } else { UploadViewModel uploadViewModel = null; if (e.ChosenPhoto != null) { uploadViewModel = new UploadViewModel { PictureStream = e.ChosenPhoto, ImageIdentifier = string.Format("{0}.{1}", Guid.NewGuid().ToString(), System.IO.Path.GetExtension(e.OriginalFileName)) }; } task.SetResult(uploadViewModel); } }; this.photoChooserTask.Completed += completedHandler; this.photoChooserTask.Show(); return task.Task; }); }