【问题标题】:how to connect base64 image and send it using multipart?如何连接base64图像并使用多部分发送?
【发布时间】:2016-11-29 10:51:02
【问题描述】:

您好,我正在使用NgimgCrop,并且我成功获得了base64 图像现在我想使用http post 请求中的多部分表单数据发送它。

我的文件上传代码看起来像这样

scope.myCroppedImage = '';
    $scope.uploadFile = function (file) {
        if (file) {
            // ng-img-crop
            var imageReader = new FileReader();
            imageReader.onload = function (image) {
                $scope.$apply(function ($scope) {
                    $scope.myImage = image.target.result;
                    $scope.profileData.data = $scope.myImage;
                    console.log($scope.profileData.data);
                });
            };
            imageReader.readAsDataURL(file);
        }
    };

有什么方法可以在 base64 之后将其转换为多部分 angular 吗?我尝试了各种链接,但没有任何效果。

【问题讨论】:

    标签: angularjs image file upload ng-img-crop


    【解决方案1】:

    您需要使用 FormData 发送:

    YourAppName.config(function($httpProvider) {
    
        $httpProvider.defaults.transformRequest = function(data) {
    
        var fd = new FormData();
        angular.forEach(data, function(key, value) {
            fd.append(key, value);
        });
    
        return fd;
    
        }
    
    });
    

    【讨论】:

    • 可以使用config()函数修改$httpProvider。我已经更新了代码。恐怕,如果您是新手,您将很难做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2016-08-18
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多