gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:26:46 26 4
gpt4 key购买 nike

我正在尝试使用 $resource 发出 HTTP 请求,它将发送与此 postman 请求相同的信息。

这是我需要复制的请求:

postman 请求:

enter image description here

postman 标题:

enter image description here

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>

Controller .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");
});
}
});
});

来 self 的请求的 header :i .堆 。 imgur 。 com/4inXR.jpg

最佳答案

在我的一个项目中,我使用了这个 Angular 模块 https://github.com/danialfarid/angular-file-upload .它非常适合我,还有演示站点。

关于javascript - AngularJS:如何通过 $resource 在 POST 中发送图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com