【问题标题】:How can I send the data from a imput type="file" to the controller?如何将数据从输入 type="file" 发送到控制器?
【发布时间】:2015-08-19 13:31:20
【问题描述】:

我在 ftl:

<input type="file" id="doc6" name="documente">

在 .js 中:

var lx = $("#longX").val();
var ly = $("#latY").val();
var jud = $("#judetPunctLucru").val();
var doc6Type = $("#doc6Type").val();
var doc6 = $('doc6').val();

validateCoords: function (lx, ly, jud, doc6Type, doc6) {
            var data = {
                lx: lx,
                ly: ly,
                jud: jud,
                doc6Type: doc6Type,
                doc6: doc6
            };
            var url = contextPath + '/validareCoord';
            return getJSONData(url, data);
        }

在控制器中:

@RequestMapping(value = "/validareCoord", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> getValidCoord(@RequestParam("lx") String lx,
                                             @RequestParam("ly") String ly,
                                             @RequestParam("jud") String jud,
                                             @RequestParam(value = "doc6Type", defaultValue = "") final String doc6Type,
                                             @RequestParam(value = "doc6", required=false) MultipartFile doc6){
.....
}

但是当我这样阅读时,doc6 不是我所需要的,错误是:

ERROR:
org.springframework.web.multipart.MultipartException: The current request is not a multipart request

你能告诉我应该如何从文件中读取数据吗?显然var doc6 = $('doc6').val(); 不行。

【问题讨论】:

    标签: java jquery ajax jquery-file-upload


    【解决方案1】:

    您应该尝试在 @RequestMapping 中添加标题:

    headers = "content-type=multipart/*"
    

    【讨论】:

    • 我认为它不适用于 method = RequestMethod.GET
    • 无论如何,var doc6 = $('doc6').val() 返回我...什么都没有
    • 好的,我解决了这个问题 var doc6=document.getElementById("doc6");
    • 那是因为你忘记了#。 var doc6 = $('#doc6').val();
    • 是的,现在是 doc6,在 JS 中没问题,但我仍然无法在控制器中获取 MultipartFile
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多