Beispiel #1
0
        public async Task <IActionResult> UploadAnh()
        {
            string folderName  = "FilesUpload\\Anh";
            string webRootPath = hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            try
            {
                int count = 0;
                foreach (var file in Request.Form.Files)
                {
                    count = count + 1;
                    var fileName        = file.FileName.Replace("\"", "").Split('.');
                    var newName         = RamdomString(20);
                    var destinationPath = Path.Combine(newPath, newName + '.' + fileName[fileName.Length - 1]);
                    while (System.IO.File.Exists(destinationPath))
                    {
                        newName         = RamdomString(20);
                        destinationPath = Path.Combine(newPath, newName + '.' + fileName[fileName.Length - 1]);
                    }
                    using (var stream = new FileStream(destinationPath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    // Coordinate viTri = ImageUtils.ReadImageLatitudeAndLongitude(destinationPath);
                    // ImageHelper.SaveResizeImage(destinationPath, 1024);

                    FileUpload fileUpload = new FileUpload
                    {
                        FileName = newName + "." + fileName[fileName.Length - 1],
                        FilePath = destinationPath,
                        FileSize = new FileInfo(destinationPath).Length.ToString(),
                        FileKey  = newName
                    };
                    fileUpload.FileType = fileName[1];
                    await _fileUploadService.CreateFileUpload(fileUpload);

                    return(new OkObjectResult(new { key = fileUpload.FileKey }));
                }
                return(new BadRequestObjectResult("Lỗi Gửi Ảnh"));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult("Lỗi Gửi Ảnh"));
            }
        }
        public async Task <IActionResult> CreateFileUpload(FileUploadDTO fileUploadDTO)
        {
            var fileUpload = fileUploadDTO.ToEntity();
            await _fileUploadService.CreateFileUpload(fileUpload);

            return(Ok(fileUpload));
        }