javascript - 使用 AngularJS 和 SpringMVC 进行分段文件上传

标签 javascript angularjs spring-mvc

我是 angularJS 的新手,尝试使用 angularJS 和 Spring MVC 上传文件,但无法获得所需的解决方案,最终在 JS Controller 中出现异常。

下面是代码,请看一下,请帮助我。谢谢

ApplicationContext.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000" /> <!-- setting maximum upload size -->
</bean>

JSP:

<div data-ng-controller='QuestionController'>
<form name="createChapForm" data-ng-submit="submitQue();" enctype="multipart/form-data" class="form-horizontal">
    <div class="form-group">
        <label class="control-label col-sm-3">Select Class * :</label>
        <div class="col-sm-8">
            <div class="col-sm-6">
                <select data-ng-model='class_id' data-ng-init='getClasses();' data-ng-change='getSubjectsClasswise(class_id);' class="form-control" required>
                    <option value="">--SELECT--</option>
                    <option data-ng-repeat='c in clss' value="{{ c.class_id}}">{{ c.class_name}}</option>
                </select>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-3">Select Subject * :</label>
        <div class="col-sm-8">
            <div class="col-sm-6">
                <select data-ng-model='question.sid' data-ng-change='doGetChapters(question.sid);' class="form-control" required>
                    <option value="">--SELECT--</option>
                    <option data-ng-repeat='s in subsss' value="{{ s.sid}}">{{ s.subject_name}}</option>
                </select>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-3">Select Chapter :</label>
        <div class="col-sm-8">
            <div class="col-sm-6">
                <select data-ng-model='question.chap_id' class="form-control" >
                    <option value="">ALL</option>
                    <option data-ng-repeat='c in chapters' value="{{ c.chap_id}}">{{ c.chap_name}}</option>
                </select>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="control-label col-sm-2" >Question :</div>
        <div class="col-sm-10 padding_0">
            <textarea data-ng-model='question.question_text' rows="5" class="form-control  " > </textarea>
            <div class="right">
                <div class="fileUpload btn btn-primary1 btn-sm">
                    <input type="file" data-ng-model="file" name="file" id="file"  id="q_id"  class="upload" />
                </div>
            </div>
        </div>
    </div>
</form>
</div>

AngularJS Controller :

$scope.submitQue = function() {
    console.log('file is ' ); console.dir(file.files[0]);
    var URL =appURL+'/adm/doAddQuestion.do';
    var fd = new FormData();
    fd.append('file', file.files[0]);
    fd.append('questionBean', angular.toJson($scope.question, true));
    $http.post(URL, fd, {
        transformRequest : angular.identity,
        headers : {
            'Content-Type' : undefined
        }
    }).success(function() {
        console.log('success');
    }).error(function() {
        console.log('error');
    });
}

Java Controller :

@RequestMapping(value = "/doAddQuestion.do", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public @ResponseBody String saveUserDataAndFile(@RequestParam(value = "file") MultipartFile file, @RequestBody QuestionBean questionBean) {
    System.out.println("output: "+questionBean.getQuestion_text());
    // Im Still wotking on it
    return "";
}

异常(exception)情况:

Mar 08, 2017 7:46:46 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotReadable
WARNING: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
 at [Source: java.io.PushbackInputStream@bc03e1; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
 at [Source: java.io.PushbackInputStream@bc03e1; line: 1, column: 3]
Mar 08, 2017 7:46:46 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
WARNING: Handler execution resulted in exception: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
 at [Source: java.io.PushbackInputStream@bc03e1; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
 at [Source: java.io.PushbackInputStream@bc03e1; line: 1, column: 3]

最佳答案

将这些 .js 文件添加到您已添加 angular.js 文件的位置 angular-file-upload.js,angular-文件上传-shim.js,ng-file-upload.js,ng-file-upload-shim.js

您可以从此链接下载Angular File For Upload.

然后在 angular.module 中添加 ngFileUpload,'angularFileUpload' 见下行。

angular.module('formSubmit', [ 'ngFileUpload',
            'angularFileUpload',  'ui.router' ]);

然后像这样在你的 Angular Controller 中添加 $upload

app.controller('FormSubmitController', function($scope, $http, $upload)

在您的 Angular 代码中使用 $upload.upload 而不是 $http.post

$upload.upload({
    url : 'doAddQuestion.do',
    file : yourFile,
    data : $scope.questionBean,
    method : 'POST'
});

改变你的 Spring Controller 。

@RequestMapping(value = "/doAddQuestion.do", method = RequestMethod.POST, headers = ("content-type=multipart/*"))
public @ResponseBody String saveUserDataAndFile(@RequestParam("file") MultipartFile file, QuestionBean questionBean) {
    System.out.println("output: "+questionBean.getQuestion_text());
            // Im Still wotking on it
    return "";
}

关于javascript - 使用 AngularJS 和 SpringMVC 进行分段文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42671510/

相关文章:

javascript - 如何为页面中的所有 anchor 链接添加平滑滚动

javascript - 这个javascript逻辑是什么意思?

javascript - Three.js 中的超链接 CSS3D 对象

angularjs - 如何将纯 Angular 应用程序作为项目添加到 Visual Studio 解决方案?

java - 如何在 Spring Boot 中发送请求范围 bean 作为响应

java - 我可以对 Post 请求使用 @Requestparam 注释吗?

javascript - 如何制作一个按钮,使其只能被拖动到一定程度?

javascript - 如何将变量注入(inject)子指令

angularjs - Electron 和 angularJS 应用程序架构 - 两个窗口中的 SPA 克隆

javascript - 无法使用 Java 和 Angular js 在一个请求中上传多张图像