Example #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));
        }
Example #2
0
        protected async Task LoadFile(IFileListEntry file)
        {
            if (!file.Type.Contains("image"))
            {
                GlobalMsg.SetMessage("Please upload image file(.png or .jpg).", MessageLevel.Error);
                return;
            }
            ;
            if (file.Size > 1000000)
            {
                GlobalMsg.SetMessage("File should be smaller than 1M.", MessageLevel.Error);
                return;
            }
            ;

            file.OnDataRead += (sender, eventArgs) => InvokeAsync(StateHasChanged);
            var model = new UploadProductLinkModel()
            {
                Id        = Item.Id,
                Image     = file.Data,
                ImageName = file.Name
            };
            var imageUrl = await Service.PostImage(model, Token);

            if (imageUrl != null)
            {
                // show the image
                Item.Image = imageUrl;
                GlobalMsg.SetMessage("The file is uploaded.", MessageLevel.Normal);
            }
            else
            {
                GlobalMsg.SetMessage("Failed to upload the file.", MessageLevel.Error);
            }
        }
Example #3
0
        protected async Task LoadFile(IFileListEntry file)
        {
            file.OnDataRead += (sender, eventArgs) => InvokeAsync(StateHasChanged);


            var model = new UploadProductLinkModel()
            {
                ProductId = Item.Id,
                Image     = file.Data,
                ImageName = file.Name
            };

            string imageUrl = await ProductLinkService.PostProductLinkAsync(model);

            if (imageUrl != null)
            {
                Item.ProductLinks.Insert(0, new ProductLinkModel()
                {
                    ProductId = model.ProductId, Address = imageUrl
                });
                GlobalMsg.SetMessage("File " + file.Name + " uploaded.", MessageLevel.Normal);
            }
            else
            {
                GlobalMsg.SetMessage("Failed to connect API.", MessageLevel.Error);
            }
        }
        //
        public async Task <string> PostImage(UploadProductLinkModel model, string token)
        {
            if (!_httpClient.DefaultRequestHeaders.Contains("Authorization") && !string.IsNullOrEmpty(token))
            {
                _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
            }
            try
            {
                var content = new MultipartFormDataContent();
                content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");

                content.Add(new StreamContent(model.Image, (int)model.Image.Length), "File", model.ImageName);
                content.Add(new StringContent(model.Id.ToString()), "Id");
                var response = await _httpClient.PostAsync("api/SubItemDetail", content);

                if (response.IsSuccessStatusCode)
                {
                    return(await response.Content.ReadAsStringAsync());
                }
                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
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));
        }
Example #6
0
        public IActionResult UploadFile([FromForm] UploadProductLinkModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

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

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

            var image = model.File;

            var endpoint        = configuration.GetValue <string>("endpoint");
            var accessKeyId     = configuration.GetValue <string>("accessKeyId");
            var accessKeySecret = configuration.GetValue <string>("accessKeySecret");
            var bucketName      = configuration.GetValue <string>("bucketName");

            var objectName = Guid.NewGuid().ToString() + Path.GetExtension(model.File.FileName);
            // Create an OSSClient instance.
            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
            var stream = image.OpenReadStream();

            // Upload a file.
            client.PutObject(bucketName, objectName, stream);
            var productLinkModel = new ProductLinkModel();

            productLinkModel.ProductId = model.ProductId;
            productLinkModel.Name      = objectName;
            productLinkModel.Type      = Path.GetExtension(model.File.FileName);
            productLinkModel.Address   = objectName;
            repo.AddProductLink(productLinkModel);
            return(Ok("https://nearnz.oss-ap-southeast-2.aliyuncs.com/" + objectName));
        }
Example #7
0
        public async Task <string> PostProductLinkAsync(UploadProductLinkModel model)
        {
            try
            {
                var content = new MultipartFormDataContent();
                content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");

                content.Add(new StreamContent(model.Image, (int)model.Image.Length), "File", model.ImageName);
                content.Add(new StringContent(model.ProductId.ToString()), "ProductId");
                var response = await _httpClient.PostAsync("api/ProductLink", content);

                if (response.IsSuccessStatusCode)
                {
                    return(await response.Content.ReadAsStringAsync());
                }
                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }