如何在使用 Croppie 裁剪后将图像保存在文件夹中(Jquery 图像裁剪器)

How to Save Images In Folder After cropping using Croppie (Jquery Image cropper)

我正在尝试使用 croppie 将图像裁剪到文件夹和图像路径后保存到数据库,但它转换为 base64 并且我也不知道如何将此数据发送到控制器以及如何将图像保存到文件夹及其数据库路径。

我已经尝试保存 file.write 功能,但它没有将图像数据发送回控制器

public ActionResult AddProduct(Tbl_Product product,HttpPostedFileBase file_photo)
        {
            string name = null;
            string ext = null;           

            if (ModelState.IsValid==true)
            {
                if (file_photo != null)
                {
                    name = Path.GetFileNameWithoutExtension(file_photo.FileName);
                    ext = Path.GetExtension(file_photo.FileName);

                    string path = Path.Combine(Server.MapPath("~/ProductImages"), name + ext);
                    file_photo.SaveAs(path);
                }

                product.ProductImage = name + ext;
                product.CreatedDate = DateTime.Now;
                _unitofwork.GetRepositoryInstance<Tbl_Product>().Add(product);
                return RedirectToAction("Product");
            }
            else
            {
                ViewBag.CategoryList = GetCategory();
                return View();
            }            
        }

我想将图像保存在文件夹和数据库路径中,但它显示 base64 图像

在 croppie 中,裁剪图像后您将得到 base64 格式的结果,将此 base64 保存在如下隐藏字段中,

$('#imagebase64').val(base64_data);

并且在您的控制器操作方法中,使用以下代码将图像存储在文件夹中。

if (product.imagebase64 != null)
{
    try
    {
         var base64Data = Regex.Match(product.imagebase64, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;

         byte[] imageBytes = Convert.FromBase64String(base64Data);

         string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss"); // You can write custom name here
         string path = Path.Combine(Server.MapPath("~/ProductImages"), filename + ".jpg");

         System.IO.File.WriteAllBytes(path, imageBytes);
    }
    catch (Exception ex)
    {

    }
}

我已经写了一个完整的 post 关于使用 croppie.js 和 c#

一个设置croppie的函数,

裁剪图像的功能,

然后通过ajax,

将图像日期发送到网络方法

将图像数据转换为图像并保存在文件夹中。

这里是 link, https://shaktisinghcheema.com/image-upload-with-cropping/

此外,您可能 运行 将图像数据发送到网络方法时出现超时问题, 这是 link 的解决方案: Error while sending base64 string through json

我希望这对解决这个问题的人有所帮助。