简体   繁体   English

无法通过Angular文件上传捕获响应

[英]Couldn't catch the response through Angular file upload

I am trying to upload a single file using POST Method and REST Calling to the server. 我正在尝试使用POST方法和REST调用将单个文件上传到服务器。 Using this following code, I am able to upload the module but It shows me some error after those uploading. 使用下面的代码,我可以上传模块,但是在上传之后,它显示了一些错误。 Can someone help me to solve this problem? 有人可以帮我解决这个问题吗?

My HTML Code : 我的HTML代码:

  <form name="myForm" enctype="multipart/form-data" >
      <input type="file" ngf-select ng-model="picFile" name="file" file="file"     
             accept="" ngf-max-size="2MB" required
             ngf-model-invalid="errorFile">
      <button ng-disabled="!myForm.$valid" 
              ng-click="uploadPic(picFile)">Submit</button>
  </form>

MY JS Code : 我的JS代码:

var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload','fileUpload', function ($scope, Upload,fileUpload) {
    $scope.uploadPic = function(file) {
      fileUpload.uploadFileToUrl(file);
    }
}]);

app.service('fileUpload', ['$http','Upload', function ($http,Upload) {
    this.uploadFileToUrl = function(file){
    file.upload = Upload.upload({
      url: 'http://localhost:8080/openmrs/ws/rest/v1/module/?',
      headers: {'Content-Type': 'multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p'},
      transformRequest: angular.identity,
      data: {file: file}
    });
     file.upload.then(function (response) {
            }, function (response) {
                if (response.status > 0)
            }, function (evt) {
            });
    }
}]);

Error Message : 错误信息 :

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: The classloader is null Module: openhmis.inventory (through reference chain: org.openmrs.module.Module["moduleActivator"]); org.springframework.http.converter.HttpMessageNotWritableException:无法编写JSON:类加载器为空模块:openhmis.inventory(通过参考链:org.openmrs.module.Module [“ moduleActivator”]); nested exception is org.codehaus.jackson.map.JsonMappingException: The classloader is null Module: 嵌套的异常是org.codehaus.jackson.map.JsonMappingException:类加载器为null模块:

The error message showed like "Could not write JSON". 错误消息显示为“无法编写JSON”。 That means It could not write the JSON type response to the request. 这意味着它无法将JSON类型响应写入请求。 Please check with your Response type and confirm Is it JSON type. 请检查您的响应类型并确认是否为JSON类型。

There are a lot of response types, that are 有很多响应类型,它们是

  1. text/html text / html的
  2. application/xhtml+xml 应用/ XHTML + xml的
  3. application/xml;q=0.9 应用/ XML; Q = 0.9
  4. image/webp, / ;q=0.8 图像/ WEBP,/ Q = 0.8

Better change your header with Accept status like this following, 最好使用如下所示的“接受”状态来更改标题,

  headers: {'Content-Type': undefined ,  'Accept': 'application/xhtml+xml;charset=UTF-8'}

It is working .... 这是工作 ....

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

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