【问题标题】:How to Save Images In Folder After cropping using Croppie (Jquery Image cropper)使用 Croppie 裁剪后如何将图像保存在文件夹中(Jquery Image cropper)
【发布时间】:2019-04-10 06:31:37
【问题描述】:

我正在尝试使用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 图像

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-5 croppie


    【解决方案1】:

    我已经写了一篇关于在 c# 中使用croppie.js 的完整帖子

    设置裁剪的函数,

    裁剪图像的功能,

    然后通过ajax将图片日期发送到web方法,

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

    这是链接, https://shaktisinghcheema.com/image-upload-with-cropping/

    另外,您在将图像数据发送到 Web 方法时可能会遇到超时问题, 这是解决方案的链接: Error while sending base64 string through json

    我希望这可以帮助解决这个问题的人。

    【讨论】:

      【解决方案2】:

      在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)
          {
      
          }
      }
      

      【讨论】:

      • 但是我怎样才能将$('#imagebase64').val(base64_data); 视图发送到控制器如果是,我需要通过控制器然后如何?
      • 现在你得到了HttpPostedFileBase文件,你可以在你的操作方法中使用string imagebase64代替这个
      • 我传递了字符串参数但它显示为空它也与我的图像名称匹配但仍然没有得到结果我的代码是public ActionResult AddProduct(Tbl_Product product,HttpPostedFileBase file_photo, string base64image) {--------some code------------}
      • 代码太长,但这是裁剪图像后的代码,它给出了一个 base 64 代码$('#cropImageBtn').on('click', function (ev) { $uploadCrop.croppie('result', { type: 'base64', format: 'jpeg', size: {width: 150, height: 200} }).then(function (resp) { $('#item-img-output').attr('src', resp); $('#cropImagePop').modal('hide'); }); });
      • 您将base64 数据存储在隐藏字段的什么位置?在您的表单中,添加一个隐藏字段&lt;input type="hidden" id="imagebase64" name="imagebase64"&gt; 并将base64 数据存储在隐藏字段中。在你的$('#item-img-output').attr('src', resp); 行之后像这样$('#imagebase64').val(resp);。
      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 2019-02-07
      • 2016-03-18
      • 2023-04-03
      • 2016-03-08
      • 2019-08-05
      • 2017-01-21
      • 1970-01-01
      相关资源
      最近更新 更多