Esempio n. 1
0
 private void SaveImagesAndLink(CarItem dbModel, IEnumerable <HttpPostedFileBase> files)
 {
     //save images into db
     if (files == null)
     {
         return;
     }
     foreach (var file in files)
     {
         if (file == null || file.ContentLength <= 0)
         {
             continue;
         }
         var image = new CarImages
         {
             Name          = file.FileName,
             ContentLength = file.ContentLength,
             ContentType   = file.ContentType,
             Data          = new byte[file.ContentLength]
         };
         file.InputStream.Read(image.Data, 0, (int)image.ContentLength);
         _carImageManager.Add(image);  //save image to db
         dbModel.CarImages.Add(image); //add link to CarItem
     }
 }
Esempio n. 2
0
 public IActionResult Add([FromForm] FileUpload file, [FromForm] int carId)
 {
     if (file.files.Length > 0)
     {
         CarImage carImage = new CarImage();
         carImage.Date = DateTime.Now;
         string path = _webHostEnvironment.WebRootPath + "\\images\\";
         if (!Directory.Exists(path))
         {
             Directory.CreateDirectory(path);
         }
         Tuple <string, string, string> tuple = GuidHelper.GuidedPath(file.files);
         string guidedPath      = tuple.Item2;
         string guidedPathForDb = tuple.Item1;
         using (FileStream fileStream = System.IO.File.Create(Environment.CurrentDirectory + @"\wwwroot\images\" + tuple.Item3))
         {
             file.files.CopyTo(fileStream);
             fileStream.Flush();
         }
         carImage.CarId     = carId;
         carImage.ImagePath = guidedPathForDb;
         var result = _carImageManager.Add(carImage);
         if (result.IsSuccessful)
         {
             return(Ok(result));
         }
         return(BadRequest(result));
     }
     return(BadRequest("Geçerli bir klasör seçiniz!"));
 }
        public IActionResult Add([FromForm(Name = ("Image"))] IFormFile file, [FromForm] CarImage carImage)
        {
            var result = _carImageManager.Add(file, carImage);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
        public IActionResult Add([FromForm] CarImage carImage, [FromForm] IFormFile formFile)
        {
            var result = _carImageManager.Add(carImage, formFile);

            if (result.Success)
            {
                return(Ok(result.Message));
            }
            return(BadRequest(result.Message));
        }
        public IActionResult Add([FromForm] CarImage carImage, [FromForm] IFormFile formFile)
        {
            if (formFile.Length > 0)
            {
                if (!Directory.Exists(_environment.WebRootPath + @"\Uploads\"))
                {
                    Directory.CreateDirectory(_environment.WebRootPath + @"\Uploads\");
                }

                string guidKey = Guid.NewGuid().ToString("N");
                using (FileStream fileStream = System.IO.File.Create(_environment.WebRootPath + GuidFileCreate(formFile, guidKey)))
                {
                    /*formdan gelen datayı belleğe yazıp oradan oluşturduğumuz dosyaya
                     * yazma işlemi yapıyoruz , ve bundan sonra cachede durması performans
                     * sorunları yaratacağıdan flush ile cache i temizliyoruz.
                     */

                    formFile.CopyTo(fileStream);
                    fileStream.Flush();

                    carImage.Date      = DateTime.Now;
                    carImage.ImagePath = GuidFileCreate(formFile, guidKey);

                    var result = _carImageManager.Add(carImage);
                    if (result.Success)
                    {
                        return(Ok(result.Message));
                    }
                    return(BadRequest(result.Message));
                }
            }
            else
            {
                return(BadRequest(Messages.FileNotRead));
            }
        }