Ejemplo n.º 1
0
 public async Task <string> Post([FromForm] FIleUploadAPI files)
 {
     if (files.files.Length > 0)
     {
         try
         {
             if (!Directory.Exists(_environment.WebRootPath + "\\uploads\\"))
             {
                 Directory.CreateDirectory(_environment.WebRootPath + "\\uploads\\");
             }
             using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads\\" + files.files.FileName))
             {
                 files.files.CopyTo(filestream);
                 filestream.Flush();
                 return("\\uploads\\" + files.files.FileName);
             }
         }
         catch (Exception ex)
         {
             return(ex.ToString());
         }
     }
     else
     {
         return("Unsuccessful");
     }
 }
Ejemplo n.º 2
0
 public async Task <string> UploadFile(Guid productId, [FromHeader] FIleUploadAPI file)
 {
     if (file.photo.ToString().Length > 0)
     {
         string path = _environment.WebRootPath.ToString();
         await _productService.AddPhotoAsync(path, productId, file.photo);
     }
     return("0");
 }
Ejemplo n.º 3
0
 public async Task <IActionResult> UploadFile([FromHeader] FIleUploadAPI file)
 {
     if (file.photo.ToString().Length > 0)
     {
         string path = _environment.WebRootPath.ToString();
         await _userService.AddPhotoAsync(path, UserId, file.photo);
     }
     return(Created($"Image/{UserId}", null));
 }
Ejemplo n.º 4
0
        public async Task <IActionResult> Upload(FIleUploadAPI files)
        {
            string msgresult = "";

            if (files.files.Length > 0)
            {
                try
                {
                    if (_environment.IsDevelopment())
                    {
                        if (!Directory.Exists(_environment.WebRootPath + "\\uploads\\"))
                        {
                            Directory.CreateDirectory(_environment.WebRootPath + "\\uploads\\");
                        }
                        using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads\\" + files.files.FileName))
                        {
                            files.files.CopyTo(filestream);
                            filestream.Flush();
                            msgresult = "\\uploads\\" + files.files.FileName;
                        }
                    }
                    else
                    {
                        var path = (_environment.WebRootPath + "\\uploads").Replace("\\", "/");

                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        using (FileStream filestream = System.IO.File.Create(path + "/" + files.files.FileName))
                        {
                            files.files.CopyTo(filestream);
                            filestream.Flush();
                            msgresult = "/uploads/" + files.files.FileName;
                        }
                    }


                    return(new OkObjectResult(new GenericResult(true, msgresult)));
                }
                catch (Exception ex)
                {
                    msgresult = ex.ToString();
                }
            }
            else
            {
                msgresult = "Unsuccessful";
            }
            return(new OkObjectResult(new GenericResult(false, msgresult)));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Put(FIleUploadAPI UplodedFile)
        {
            var  uuid            = Guid.Parse(User.Identity.Name);
            User userFromTokenId = await _context.Users.FirstOrDefaultAsync(u => u.Id == uuid);

            if (userFromTokenId == null)
            {
                return(Unauthorized());
            }

            if (UplodedFile.files == null)
            {
                return(BadRequest("No files found".ToBadRequest()));
            }
            var type = UplodedFile.files.ContentType;

            if (type != "image/jpeg" && type != "image/png" && type != "application/x-jpg")
            {
                return(BadRequest("Only image supported".ToBadRequest()));
            }
            try {
                string name = await AmazonS3Helper.SaveImageToBucket(UplodedFile.files);

                if (string.IsNullOrEmpty(name))
                {
                    return(BadRequest("Save failed".ToBadRequest()));
                }
                userFromTokenId.SetAvatar(name);
                _context.Update(userFromTokenId);
                await _context.SaveChangesAsync();

                _log.Information("User {Email} has changed his/her avatar_url on {date} ", userFromTokenId.Email, DateTime.Now);
                return(Created("img", Format.ToMessage(userFromTokenId.ToMessage(), 201)));
            } catch (Exception e) {
                _log.Fatal(e.Message + "on Put User on {Date}", DateTime.Now);
                return(StatusCode(500));
            }
        }