bootstrap fileInput控件上传文件



文件上传



















JS


$(function () {
    initFileInput("input-id");
})

function initFileInput(ctrlName) {

    var control = $('#' + ctrlName);
    control.fileinput({
        language: 'zh', //设置语言
        // uploadUrl: "/fileUpload?_csrf=" + $('#csrf').val(), //上传的地址
        uploadUrl: "/uploadFile?_csrf=" + $('#csrf').val(), //上传的地址
        allowedFileExtensions: ['xls', 'xlsx'],//接收的文件后缀
        uploadAsync: true, //默认异步上传
        showUpload: true, //是否显示上传按钮
        showRemove: true, //显示移除按钮
        showPreview: true, //是否显示预览
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        enctype: 'multipart/form-data',
        validateInitialCount: true,
        previewFileIcon: "",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",

    }).on('filepreupload', function (event, data, previewId, index) {     //上传中
        var form = data.form, files = data.files, extra = data.extra,
            response = data.response, reader = data.reader;
        console.log('文件正在上传');
    }).on("fileuploaded", function (event, data, previewId, index) {    //一个文件上传成功
        var form = data.form, files = data.files, extra = data.extra,
            response = data.response, reader = data.reader;

        var res = data.response;
        $("#path").val(res)
        console.log("文件上传成功")

    }).on('fileerror', function (event, data, msg) {  //一个文件上传失败
        console.log('文件上传失败!' + data.id);


    })
}

$("#add_excel_info").click(function () {

    var category = $("#category").val();
    var sheet_id = $("#sheet-id").val();
    var path = $("#path").val();

    $.ajax({
        url: '/',
        type: 'GET',
        // headers: {'Content-Type': 'application/json'},
        data: {
           xxxxx
        },
        success: function (data) {
            alert("添加成功!");
            get_magazine_awards();
        }
    });
});

Controller

 @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(HttpServletRequest request,@RequestParam(value = "uploadFile") MultipartFile uploadFile) throws IOException {
        if(uploadFile.isEmpty()){
            return "false";
        }
        String fileName = uploadFile.getOriginalFilename();

        String path = "F:\\";
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            uploadFile.transferTo(dest); //保存文件
            return path;
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }

 

你可能感兴趣的:(bootstrap fileInput控件上传文件)