使用PUT上传AngularJS图片,可能如何? [英] AngularJS image upload with PUT, possible, how?

查看:118
本文介绍了使用PUT上传AngularJS图片,可能如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AngularJS通过PUT或Post请求上传图像,如果可能的话,如下所示如何不更改标头(仍然是json)并且没有有效载荷.

I'm trying to use AngularJS do upload an images with a PUT or Post request, is this possible, if so how as below does not change the header (still json) and there is no payload.

这是我尝试过的:

在我的控制器中:

       $scope.uploadFile = function (elm) {

            $scope.files = elm.files
            $scope.$apply();

            var fd = new FormData()
            fd.append('file', $scope.files[0])


            $scope.profile

                .customPOST(fd, "", ["Content-Type", undefined])
             .withHttpConfig({
                    transformRequest: angular.identity
                });

            event.preventDefault();


        };

html:

<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>

此代码实际上获取文件名并将其添加到数据库中,但是在放置请求期间实际上没有文件被上传.

This code actually gets the file name and add its to the database, but no file actually gets uploaded during the put request.

推荐答案

您可以使用像这样的角度方法,HTML部件

You can use angular method like this, HTML Part,

<input type="file" ng-file-select="onFileSelect($files)" />

在您的控制器中写下该内容,

In your controller write this,

    $scope.onFileSelect = function ($files) {
        var file = $files[0];
        // upload file to server
        $scope.upload = $upload.upload({
            url: 'http://www.yourdomain.com/yourpath',
            file: file
        }).success(function(data, status, headers, config) {
            // file is uploaded successfully
        }).error(function (data) {
            // file upload failed
        });
    };

这篇关于使用PUT上传AngularJS图片,可能如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆