ajax提交包含file的表单

本项目基于spring mvc框架,是一个maven项目;

主要是利用FormData来提交表单;

首先在pom.xml加入依赖:

项目代码:http://download.csdn.net/detail/liujan511536/9489630



            org.springframework
            spring-webmvc
            4.2.1.RELEASE
        
        
            commons-fileupload
            commons-fileupload
            1.3
        
        
            org.apache.commons
            commons-lang3
            3.3.2
        
        
            servlet-api
            servlet-api
            2.5
        

然后在applicaitonContext.xml中加入:

 
        
        
    


前端jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    


file:
name:


后台controlelr代码:

package com.liujan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * Created by liujan on 4/12/16.
 */
@Controller
public class HelloController {
    @RequestMapping(value = "/")
    public String show() {
        return "upload";
    }

    @RequestMapping(value = "uploadfile.html " )
    @ResponseBody
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file,
                         @RequestParam("name") String name) {
        if (name != null) {
            String fileName = file.getOriginalFilename();
            System.out.println("name:" + name + " file:" + fileName);
        }

       return "good";
    }
}


你可能感兴趣的:(ajax,ajax)