简体   繁体   English

上传不带AngularJS格式的图片Laravel

[英]Upload image Laravel without form AngularJS

I need upload image with AngularJS without form. 我需要使用不带表单的AngularJS上传图像。 but I don't know how to receive it in Laravel, send it. 但是我不知道如何在Laravel中接收它,然后发送它。 My code is this: 我的代码是这样的:

<input type="file" name="file" accept="image/jpeg, image/png" id="file" ng-model="data.image">


$('input[type=file]').change(function () {
    $scope.img = this.files[0];
    var filePath = $("#file").val();
    var reader = new FileReader();
    reader.onload = function (e) {
        $('#image').attr('src',e.target.result);
        $scope.img["imgbase64"] = e.target.result;
    };
    reader.readAsDataURL(this.files[0]); 
});

I use the service here: 我在这里使用该服务:

        var imgSend = new FormData();
        imgSend.append("file",$scope.img);
        data["image"] = imgSend;
            url = "maquinas";    registroMaquinaServices.servicesRegistroMaquinaPost(url,data).then(function(promise){
                    var requests = promise.data.response;
                    console.log(requests);
                })

I'm sending this to laravel. 我将其发送给laravel。

在此处输入图片说明

Thanks. 谢谢。

You can use https://github.com/ghostbar/angular-file-model to handle your image file for uploading and then you will be able to create form object and send it to your laravel server to be able to get the image file as regular uploaded file, not a base64 encoded one. 您可以使用https://github.com/ghostbar/angular-file-model来处理要上传的图像文件,然后您将能够创建表单对象并将其发送到laravel服务器以获取图像文件作为常规上传文件, 而不是 base64编码的文件。 this is the code i use to do so in the example all of the data that needs to be sent to server is in $scope.data variable 这是我在示例中使用的代码,所有需要发送到服务器的数据都在$scope.data变量中

$http({
    headers: {
        'Content-Type': undefined //undefined value is on purpose
    },
    method: method,
    url: url,
    data: $scope.data,
    transformRequest: function(data, headersGetter) {

        var formData = new FormData();

        angular.forEach(data, function(value, key) {
            formData.append(key, value);
        });

        return formData;
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM