Exemple #1
0
        public async Task <IActionResult> UploadFileAsync([FromForm] UploadProductLinkModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (model.Id == 0)
            {
                ModelState.AddModelError("ProductId", "ProductId shouldn't be zero");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //
            string sasUrl = null;

            if (model.File != null)
            {
                using (var stream = model.File.OpenReadStream())
                {
                    var imageId = await imageStore.SaveImage(stream);

                    sasUrl = imageStore.UriFor(imageId);
                }
            }
            await repo.UpdateImageAsync(model.Id, sasUrl);

            return(Ok(sasUrl));
        }
Exemple #2
0
        public async Task <IActionResult> PostAImage(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "SubItemDetailPostAImage")] HttpRequest req, ILogger log)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            var model = JsonConvert.DeserializeObject <UploadProductLinkModel>(requestBody);

            if (model == null)
            {
                return(new BadRequestObjectResult("can't convert the input to a model"));
            }

            if (model.Id < 0)
            {
                return(new BadRequestObjectResult("ProductId shouldn't be smaller than zero"));
            }
            string sasUrl = null;

            if (model.File != null)
            {
                //var isAzure = configuration.GetValue<string>("IsAzure");
                //if (isAzure.ToUpper() == "YES")
                //{
                //    using (var stream = model.File.OpenReadStream())
                //    {
                //        var imageId = await imageStore.SaveImage(stream);
                //        sasUrl = imageStore.UriFor(imageId);
                //    }
                //}
            }
            await repo.UpdateImageAsync(model.Id, sasUrl);

            return(new OkObjectResult(sasUrl));
        }
Exemple #3
0
        public async Task <IActionResult> UploadFileAsync([FromForm] UploadProductLinkModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (model.Id == 0)
            {
                ModelState.AddModelError("ProductId", "ProductId shouldn't be zero");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //
            string sasUrl = null;

            if (model.File != null)
            {
                var isAzure = configuration.GetValue <string>("IsAzure");
                if (isAzure.ToUpper() == "YES")
                {
                    using (var stream = model.File.OpenReadStream())
                    {
                        var imageId = await imageStore.SaveImage(stream);

                        sasUrl = imageStore.UriFor(imageId);
                    }
                }
                else
                {
                    var filePath = Path.GetTempFileName();
                    if (model.File.Length > 0)
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            model.File.CopyToAsync(stream).Wait();
                        }
                    }
                    var objectName = Guid.NewGuid().ToString() + Path.GetExtension(model.File.FileName);

                    System.IO.File.Copy(filePath, Path.Combine(env.ContentRootPath, "Uploaded", objectName));
                    sasUrl = objectName;
                }
            }
            await repo.UpdateImageAsync(model.Id, sasUrl);

            return(Ok(sasUrl));
        }