Exemplo n.º 1
0
        public JsonResult AddPhoto(string path)
        {
            List <IFormFile> Photos = Request.Form.Files.ToList();
            var photo_info          = new List <object>();

            if (Checker.CheckList(Photos) && path != null)
            {
                for (int i = 0; i < Photos.Count; i++)
                {
                    if (PhotoUpload.IsValid(Photos[i]))
                    {
                        string filename = (DateTime.Now.ToShortDateString() + Photos[i].FileName).Replace(" ", "").Replace("/", "");
                        string _path    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", path, filename);
                        if (PhotoUpload.HasPhoto(_path))
                        {
                            Guid guid = new Guid();
                            _path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", path, guid + filename);
                        }

                        PhotoUpload.UploadTo(Photos[i], _path);
                        var obj = new
                        {
                            Filename = filename,
                            Url      = $"/Admin/Uploads/{path}/" + filename
                        };
                        photo_info.Add(obj);
                    }
                    else
                    {
                        return(Json(new
                        {
                            response = 400
                        }));
                    }
                }
                return(Json(new
                {
                    status = 200,
                    data = photo_info
                }));
            }
            else
            {
                return(Json(new { error = "something went wrong" }));
            }
        }
Exemplo n.º 2
0
        public JsonResult AddPhoto(string path)
        {
            List <IFormFile> Photos = Request.Form.Files.ToList();
            var photo_info          = new List <object>();

            if (Checker.CheckList(Photos) && path != null)
            {
                for (int i = 0; i < Photos.Count; i++)
                {
                    if (PhotoUpload.IsValid(Photos[i]))
                    {
                        string filename  = (DateTime.Now.ToShortDateString() + Photos[i].FileName).Replace(" ", "").Replace("/", "");
                        string _path     = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", path, filename);
                        string thumbpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", "Thumbnails", filename);
                        if (PhotoUpload.HasPhoto(_path))
                        {
                            Guid guid = new Guid();
                            _path     = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", path, guid + filename);
                            thumbpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Admin", "Uploads", "Thumbnails", guid + filename);
                        }

                        PhotoUpload.UploadTo(Photos[i], _path);

                        #region Resize Image
                        //using (Image<Rgba32> image = SixLabors.ImageSharp.Image.Load(_path))
                        //{
                        //    image.Mutate(x =>
                        //    {
                        //        x
                        //        .Resize(168, 168);
                        //    });
                        //    image.Save(thumbpath);
                        //}
                        #endregion

                        var obj = new
                        {
                            Filename = filename,
                            Url      = $"/Admin/Uploads/{path}/" + filename
                        };
                        photo_info.Add(obj);
                    }
                    else
                    {
                        return(Json(new
                        {
                            response = 400
                        }));
                    }
                }
                return(Json(new
                {
                    status = 200,
                    data = photo_info
                }));
            }
            else
            {
                return(Json(new { error = "something went wrong" }));
            }
        }