Пример #1
0
        public async Task <EasyBlobModel> UploadBlobAsync(string imageNameString, byte[] blobBytes)
        {
            var containerReference = _cloudBlobClient.GetContainerReference(_containerNameString);
            var blobReference      = containerReference.GetBlockBlobReference(imageNameString);

            EasyBlobModel easyBlobModel        = null;
            string        blobExceptionMessage = string.Empty;

            try
            {
                await blobReference.UploadFromByteArrayAsync(blobBytes, 0, blobBytes.Length);
            }
            catch (Exception ex)
            {
                blobExceptionMessage = ex.Message;
            }
            finally
            {
                easyBlobModel = new EasyBlobModel()
                {
                    BlobName             = imageNameString,
                    BlobExceptionMessage = blobExceptionMessage
                };
            }

            return(easyBlobModel);
        }
        private async Task UploadImageAsync(string imageNameString, string extensionString,
                                            byte[] imageBytesArray)
        {
            using (var httpClient = new HttpClient())
            {
                var fileNameString = string.Concat(imageNameString, ".", extensionString);
                var imgae64String  = Convert.ToBase64String(imageBytesArray);
                var easyBlobModel  = new EasyBlobModel()
                {
                    BlobNameString     = fileNameString,
                    BlobContentsString = imgae64String
                };
                var easyBloModelString = JsonConvert.SerializeObject(easyBlobModel);

                httpClient.BaseAddress = new Uri("https://easymobileapp.azurewebsites.net/");

                using (var stringContent = new StringContent(easyBloModelString, Encoding.UTF8))
                {
                    stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var httpResponse = await httpClient.PutAsync($"api/blob/{imageNameString}/{extensionString}", stringContent);

                    var responseString = await httpResponse.Content.ReadAsStringAsync();

                    Console.WriteLine(responseString);
                }
            }
        }
Пример #3
0
        public async Task <HttpResponseMessage> Put(string blobNameString, string typeString,
                                                    [FromBody] EasyBlobModel uploadBlobModel)
        {
            var blobBytes       = Convert.FromBase64String(uploadBlobModel.BlobContents);
            var imageNameString = string.Concat(blobNameString, ".", typeString);
            var easyBlobModel   = await _blobService.UploadBlobAsync(imageNameString, blobBytes);

            var responseMessage = PrepareResponse(easyBlobModel);

            return(responseMessage);
        }
Пример #4
0
        private HttpResponseMessage PrepareResponse(EasyBlobModel easyBlobModel)
        {
            HttpResponseMessage responseMessage = null;

            if (string.IsNullOrEmpty(easyBlobModel.BlobExceptionMessage) == false)
            {
                responseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                              easyBlobModel.BlobExceptionMessage);
            }
            else
            {
                responseMessage = Request.CreateResponse(HttpStatusCode.OK, easyBlobModel);
            }

            return(responseMessage);
        }
Пример #5
0
        public async Task <EasyBlobModel> DownloadBlobAsync(string imageNameString)
        {
            var containerReference = _cloudBlobClient.GetContainerReference(_containerNameString);
            var blobReference      = containerReference.GetBlockBlobReference(imageNameString);

            byte[]        blobBuffer           = null;
            EasyBlobModel easyBlobModel        = null;
            string        blobExceptionMessage = string.Empty;
            string        blobEncodedString    = string.Empty;

            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    await blobReference.DownloadToStreamAsync(memoryStream, _tokenSource.Token);

                    blobBuffer        = memoryStream.GetBuffer();
                    blobEncodedString = Convert.ToBase64String(blobBuffer, 0, blobBuffer.Length);
                }
                catch (Exception ex)
                {
                    blobExceptionMessage = ex.Message;
                }
                finally
                {
                    easyBlobModel = new EasyBlobModel()
                    {
                        BlobContents         = blobEncodedString,
                        BlobName             = imageNameString,
                        BlobExceptionMessage = blobExceptionMessage
                    };
                }

                return(easyBlobModel);
            }
        }