javascript - 带有formdata和其他字段控制的http post不会转到java
问题描述
我是 AngularJs 的新手。我正在尝试通过 Jax-RS post rest API 上传图像文件(.bmp、.jpg 等)及其标签,但我控制不从我的 angularjs 控制器进入 java post api。
调试时控件没有进入 java 文件。您能否帮助理解我的代码中有什么问题。
我的文件.html
Label of File: <input type="text" name="iconlabel" data-ng-model="imporLabelName"><br>
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
<button type="button" class="btn btn-primary pull-right" data-ng-click="uploadfile();">Submit
</button>
我的文件控制器
define([ '{angular}/angular' ], function(angular) {
var module = angular.module('myFile', [ 'ngResource', 'colorpicker-dr' ]);
module.controller('myFileController', [ '$scope', '$sce', '$http', '$location', '$window', 'w20MessageService'
,function($scope, $sce, $http, $location, $window, w20MessageService) {
var config = {
headers: {
"Content-Type": undefined,
}
};
/*** Modale for MyFile **/
$scope.openMyFile = function() {
$("#myfile").modal("show");
};
$scope.uploadfile = function() {
$scope.file = document.getElementById('myFile').files[0];
alert('LabelName = '+$scope.imporLabelName);
var formData = new $window.FormData();
formData.append("label", $scope.imporLabelName);
formData.append("file", $scope.file);
alert('File = '+$scope.file);
var url = "uploadfile/label="+$scope.imporLabelName;
alert("URL = "+url);
$http.post(url,$scope.formData,config).success(function(response) {
$scope.result = "SUCCESS";
}).error(function(response, status) {
if (status === 400) {
w20MessageService.addMessageErreur(data.message, "msgGererParam");
} else {
w20MessageService.addMessageErreur("eox.message.error.load.traitement", "msgGererParam");
}
});
$("#myfile").modal("hide");
};
} ]);
return {
angularModules : [ 'digitalJes' ]
};
});
java API 代码
@POST
@Path("/uploadfile/{label}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@PathParam("label") String label, @FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInputDetails) {
CacheControl cc = new CacheControl();
cc.setNoCache(true);
return Response.ok(uploadFileUtil.uploadFile(label, fileInputStream, fileInputDetails)).cacheControl(cc).build();
}
错误代码和错误信息
HTTP 状态 400 – 错误请求
由于某些被认为是客户端错误(例如,格式错误的请求语法、无效的请求消息帧或欺骗性请求路由),服务器不能或不会处理请求。
解决方案
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInputDetails
似乎fileInputStream
和fileInputDetails
变量都是从file
参数初始化的。html
该领域还有
...
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
在这里id="myFile"和name="myfile"并且您将这些作为"@FormDataParam("file")"接收。
推荐阅读
- deep-learning - Google Colab KeyError:“COLAB_TPU_ADDR”
- python - 如何每次比以前多移动一次列表?
- multithreading - 多个进程可以在多线程单核 CPU 上并行运行吗?
- python - 为什么我在 django 上的 html 文件中出现 endfor 加载错误?
- sublimetext3 - Sublimetext 3 键绑定到 PackageControl 包中的特定命令
- mysql - 如果 count=0,如何包含 SQL 计数的结果?
- rust - 如何限制 futures::Stream::concat2 读取的字节数?
- android - 为什么所有的arraylist都存储在数据库中?
- mysql - 带有 node/express 和 mysql 的 mvc
- python - Openpyxl:“权限被拒绝”但 Excel 工作表未打开