angularjs - 使用 AngularJS 将图像上传到 Web Api
问题描述
嗨,我是 Web API 的新手,我正在使用 AngularJS 和 Web API 我正在尝试将图像上传到 Web API,但是当我单击按钮发送图像时,它给了我一个错误
我已经使用了 GET 方法,它向我显示了该 Web API 中的所有图像,但它不适用于 POST 方法
这是我的代码
索引.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" href="css/bootstrap.css" media="screen">
<link rel="stylesheet" href="css/bootswatch.min.css">
</head>
<body>
<div ng-app="MarcasApp" >
<div ng-controller="MarcaController">
<div class="container">
<h2>Muestra</h2>
</div>
<input type="button" class="btn btn-info" value="Todos" ng-click="getTodos()">
<form name="formulario" method="post" action="" enctype="multipart/form-data">
<div class="container">
<label for="caption">Id:</label>
<input type="input" class="form-control" ng-model="txtId"/>
</div>
<div class="container">
<label for="caption">Cargar Marcar:</label>
<input type="file" ng-files="getTheFiles"/>
</div><br>
<div class="container">
<input type="submit" value="Enviar Marca" class="btn btn-success"
ng-click="btnGuardar()"/>
</div><br>
<div class="container">
Mensaje:{{msgGuardar}}
</div>
</form>
<div class="panel panel-info">
<ul class="list-group" ng-repeat="marca in marcas">
<li class="list-group-item">
<img src="{{marca.imagen}}">
{{marca.marca1}} + {{marca.marcaId}}
</li>
</ul>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/angular.min.js"></script>
<script src="MarcaJson.js"></script>
</body>
</html>
MarcaJson.js
var app = angular
.module("MarcasApp", [])
.controller("MarcaController", function($scope, $http){
$scope.getTodos =function(){
var apiUrl = 'http://tiendawebapi.azurewebsites.net/api/marcas/';
$http.get(apiUrl).then(function(response){
$scope.marcas = response.data;
});
};
$scope.btnGuardar = function(){
var datos = {
id: $scope.txtId,
imagen: $scope.getTheFiles
}
var apiUrl = 'http://tiendawebapi.azurewebsites.net/api/marcas/';
$http.post(apiUrl, JSON.stringify(datos)).then(function(response){
if (response.statusText == Created){
$scope.msgGuardar = "Marca Guardada";
}
}, function(error){
$scope.msgGuardar = "Ocurrio un error " + error.statusText;
});
};
});
解决方案
推荐阅读
- python - 我的 Azure Function App 的 API 给了我错误代码 500
- azure - 使用 .net 创建 AD 应用程序
- google-cloud-vision - Cloud Vision 在徽标检测上返回中文名称
- excel - 如何访问 Excel 中的一系列行?
- javascript - 从每个创建列表
- xcode - Xamarin 窗体 iOS 应用程序在应用程序安装和启动后停止调试,并且没有任何错误消息
- unreal-engine4 - 几秒钟后如何让我的门恢复到原来的关闭状态
- android - 从 ImageView 中的图像中选择颜色 [问题]
- typescript - 如何使用 TypeScript 调试使用 Expo 创建的 React Native 项目
- unity3d - 如何在没有fps下降的情况下统一检查互联网连接