示例#1
0
        public async Task <IActionResult> AddImage(IFormFile imageFile)
        {
            string id = (string)RouteData.Values["id"];

            if (!Request.HasFormContentType)
            {
                return(new UnsupportedMediaTypeResult());
            }

            var filename = $"{id}.jpg";

            Stream imageStream = null;

            try
            {
                imageStream = imageFile.OpenReadStream();

                var blobRef = await blobService.UploadBlobAsync(filename, imageStream);

                await docService.AddImageToProductAsync(id, blobRef);
            }catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
            }

            return(Ok());
        }
        public async Task <IActionResult> AddProductImage(IFormFile imageFile)
        {
            try
            {
                _logger.LogInformation("AddProductImage is called");

                string id     = (string)RouteData.Values["id"];
                string camera = (string)RouteData.Values["camera"];

                if (!Request.HasFormContentType)
                {
                    return(new UnsupportedMediaTypeResult());
                }

                //BLOB Service: Get blob to write
                string blobFileRef;
                using (var imageStream = imageFile.OpenReadStream())
                {
                    blobFileRef = await _blobStorageService.UploadBlobAsync($"{id}.jpg", imageStream);
                }

                //update cosmos db with image link
                await _docService.AddImageToProductAsync(id, camera, blobFileRef);

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError("Error happened while calling AddProductImage", ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }