javascript - AngularJS: How do I send an image file in POST via $resource? -
i trying make http request using $resource, send same information postman request does. of course, not doing right.
here request need replicate:
postman request: http://i.stack.imgur.com/9zgnf.jpg
postman headers: http://i.stack.imgur.com/org0m.jpg
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), uncomment below , remove 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 404 during development) --> <script src="cordova.js"></script> <!-- 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"); }); } }); });
headers request: i . stack . imgur . com/4inxr.jpg (sorry, had put spaces in link cause can't send more 2)
please assume i'm borderline retarded
in 1 of project use angular module https://github.com/danialfarid/angular-file-upload. works me well, there demo site.
Comments
Post a Comment