繁体   English   中英

AngularJS:如何通过$ resource在POST中发送图像文件?

[英]AngularJS: How do I send an image file in POST via $resource?

我正在尝试使用$ resource发出HTTP请求,该请求将发送与邮递员请求相同的信息。

这是我需要复制的请求:

邮递员要求:

在此处输入图片说明

邮递员标题:

在此处输入图片说明

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>File Upload</title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="lib\ionic\js\angular-file.js"></script>
    <script src="lib\ionic\js\angular-resource.js"></script>
  </head>
  <body ng-app="starter" ng-controller="FileController">
    <input type="file" ng-model="model.image" change="upload(model)"/>
  </body>
</html>

controllers.js

angular.module('starter.controllers',[])

.controller('FileController', function ($scope, $resource) {
    var Files = $resource('http://api.artistappz.com/api/v1/cover/x-app-id/3865f620590f40d493ec8d900b4c24d3/', null, {
        post: {
            method:'POST'
        }
    });

            angular.extend($scope, {

                model: { image: null },

                upload: function(model) {
                    console.log("File chosen");
                    console.log(model);
                    Files.prototype.$post.call({},model.image, function(self, headers) {
                        // Handle server response
                        console.log("DONE");
                    });
                }
            });
});

我要求的标题: i。 堆栈。 imgur。 COM / 4inXR.jpg

在我的一个项目中,我使用了这个角度模块https://github.com/danialfarid/angular-file-upload 它对我来说很好用,也有演示站点。

暂无
暂无

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

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