【问题标题】:How to send file using ajax file upload to controller along with other parameters like textbox data in spring mvc?如何使用ajax文件上传将文件与spring mvc中的文本框数据等其他参数一起发送到控制器?
【发布时间】:2016-12-10 03:10:44
【问题描述】:

还需要将json中的数据与文件一起发送到服务器端。如何在服务器端和客户端处理这个问题?

【问题讨论】:

  • 没找到你吗?您需要使用 ajax 在服务器上上传文件,并且还想在该 ajax 调用中发送一些 json 数据??
  • 嘿发送数据,您可以使用以下库zurb.com/playground/ajax-upload
  • 连同用户选择的文件,我需要将一些参数(用户输入的文本框的值)发送到服务器端?那么如何同时做到这两点

标签: java jquery spring spring-mvc


【解决方案1】:

据我所知,您可以只发送带有文件的文本,而不是 JSON 格式或二进制文件。 你可以用邮递员检查它们! 后端有一个Controller示例:

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/uploadImageGallery", method = RequestMethod.POST)
public ResponseEntity<JPresent<Boolean>> imageGallery(@RequestParam("file") MultipartFile file,
                                                      @RequestParam("marketerId") Long marketerId) {

        Boolean result = new Boolean(false);

        MarketerPictureModel pictureModel = new MarketerPictureModel();
        byte[] bytes;
        if (!file.isEmpty()) {
            try {
                pictureModel.setContentType(file.getContentType());
                service.addToImageGallery(pictureModel);
                return jpresent.getResponse(result = true);
            } catch (IOException e) {
                e.printStackTrace();
                return ...
            } catch (Exception e) {
            ...

    }

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2013-12-03
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多