directory

Page code

Js code

The background code

Depend on the configuration


Page code

<form id="form1" enctype="multipart/form-data"> id_token: <input type="text" id="id_token" value="qweasd123"> 图 :<input type="file" id="file" name="imgfile"> Onclick ="upload()" value=" upload "> </form>Copy the code

Js code

function upload() { var formData = new FormData(); var file = document.getElementById('file').files[0]; var id_token = $('#id_token').val(); formData.append("file", file); formData.append("id_token", id_token); $. Ajax ({url: "/ajax/upload", type: "post", data: formData, dataType: "json", cache: false,// Upload file without caching */ success: function (data) {console.log(data.file); // Success: function (data) {console.log(data. console.log(data.id_token); }}); }Copy the code

 

The background code

@ResponseBody
@RequestMapping(value = "/ajax/upload",method = RequestMethod.POST)
public String upload(MultipartFile file,String id_token) {
    JSONObject json = new JSONObject();
    try {

        System.out.println(file.getOriginalFilename());
        System.out.println(id_token);
        json.put("file", file.getOriginalFilename());
        json.put("id_token", id_token);
    } catch (Exception e) {
        logger.error("AjaxImgController.upload error:", e);
    }
    return json.toString();
}
Copy the code

 

Depend on the configuration

<! - file upload Settings - > < bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.Com monsMultipartResolver" > <property name="maxUploadSize" value="10000000"/> </bean> <dependency> <groupId>commons-io</groupId> < artifactId > Commons - IO < / artifactId > < version > 2.4 < / version > < / dependency > < the dependency > < the groupId > Commons fileupload - < / groupId > < artifactId > Commons fileupload - < / artifactId > < version > 1.3.3 < / version > < / dependency >Copy the code