【问题标题】:How to crop image using croppie js and upload in asp.net mvc如何使用croppie js裁剪图像并在asp.net mvc中上传
【发布时间】:2016-10-27 00:38:18
【问题描述】:

您好,我想裁剪图像并将其上传到服务器上。

我正在使用 croppie js 插件并使用 get() 方法通过 WebImage 类获取点以在服务器上对其进行裁剪。

Asp.net MVC 代码

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ImageCrop(FormCollection fc)
    {
                WebImage data = WebImage.GetImageFromRequest();
                if (data != null)
                {
                    int x1, y1, x2, y2;
                    x1 = int.Parse(fc["x1"].ToString());
                    y1 = int.Parse(fc["y1"].ToString());
                    x2 = int.Parse(fc["x2"].ToString());
                    y2 = int.Parse(fc["y2"].ToString());
                    var fileName = Path.GetFileName(data.FileName);
                    fileName = Lawyer_id2 + ".jpeg";
                    var big = Server.MapPath("~/contents/ProfilePictures/big/" + fileName);
                    data.Crop(y1, x1, x2, y2);
                    data.Save(big);

                }

      }

JS代码

$uploadCrop = $('#upload-demo').croppie({
    viewport: {
        width: 200,
        height: 200,
        type: 'square'
    },
    boundary: {
        width: 300,
        height: 300
    },
    showZoomer: false,
    mouseWheelZoom: false
});
readFile(fl);
$(".closeModal").on("click", function () {
    $uploadCrop.croppie('result', {
        type: 'canvas',
        size: 'viewport'
    }).then(function (resp) {
        $('.upload-msg').css('display', '');
        popupResult({
            src: resp
        });

    });
    var arr = $uploadCrop.croppie('get').points;
    $("#x1").val(arr[0]);
    $("#y1").val(arr[1]);
    $("#x2").val(arr[2]);
    $("#y2").val(arr[3]);
});  

我在隐藏的输入字段中获取所有点,然后将这些点传递给 webimge 对象进行裁剪,但问题是裁剪后的图像没有保持纵横比并且裁剪错误,浏览器端裁剪是完美的,但是当我将它传递给服务器端进行裁剪时,它不像浏览器端那样工作,我不知道如何解决这个问题。

【问题讨论】:

  • 你做了两次同样的工作......为什么?
  • @Hackerman 我需要在服务器端进行裁剪,但我不知道如何使用croppie 上传服务器端
  • 基于这个例子,你只需要上传result-image:foliotek.com/devblog/…

标签: javascript jquery asp.net asp.net-mvc


【解决方案1】:

裁剪已经在客户端进行,您应该只将结果发送到服务器端。无需向服务器发送裁剪点。

在html上定义SelectUpload按钮,以及一个带有id="main-cropper"的div

        <div>
            <div>
                <div id="main-cropper"></div>
                <a class="button actionSelect">
                    <input type="file" id="select" value="Choose Image" accept="image/*">
                </a>
                <button class="actionUpload">Upload</button>
            </div>
        </div>

在JS代码上,将croppie对象附加到dive,定义视点边界,最后向服务器发送请求以将结果存储为blob。在服务器端,假设将有一个 Create 控制器和 GetImage 操作等待请求。 testFileName.png 被指定为文件名,您可以根据自己的场景进行修改。

        var basic = $('#main-cropper').croppie({
            viewport: { width: 200, height: 300 },
            boundary: { width: 300, height: 400 },
            showZoomer: false
        });

        function readFile(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#main-cropper').croppie('bind', {
                        url: e.target.result
                    });
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

        $('.actionSelect input').on('change', function () { readFile(this); });
        $('.actionUpload').on('click', function() {
            basic.croppie('result','blob').then(function(blob) {
                var formData = new FormData();
                formData.append('filename', 'testFileName.png');
                formData.append('blob', blob);
                var MyAppUrlSettings = {
                    MyUsefulUrl: '@Url.Action("GetImage","Create")'
                }

                var request = new XMLHttpRequest();
                request.open('POST', MyAppUrlSettings.MyUsefulUrl);
                request.send(formData);
            });
        });

在服务器上,在Create 控制器中:

    [HttpPost]
    public ActionResult GetImage(string filename, HttpPostedFileBase blob)
    {
        var fullPath = "~/Images/" + filename;
        blob.SaveAs(Server.MapPath(fullPath));
        return Json("ok");
    }

生成uuid字符串也很有意义,并将其设置为文件名并将其存储在数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2015-08-10
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多