在上传文件的同时 传递参数 (ajax上传文件并传值给后台)

之前用的ajax方法上传文件,结果这边需要对上传文件进行分类和关键字处理,想了一下,还是不想麻烦

话不多说直接上代码了

前端html以及js部分

<fieldset>
    <legend>文件信息</legend>
    <table class="table" style="width: 100%;">
        <tr>
            <th style="width: 20%;">
                文件:
            </th>
            <td>
                <input id="file" name="file" type="file" style="width:300px;height: 25px">
            </td>
        </tr>
        <tr>
            <th>关键字:</th>
            <td>
                <input id="keywords"  style="width:300px;height: 20px"/>
            </td>
        </tr>
        <tr>
            <th>分类:</th>
            <td>
                <input id="fileType"  style="width:300px;height: 25px">
            </td>
        </tr>
    </table>
</fieldset>

function uploadFile() {

            var myform = new FormData();

            myform.append('file', $("#file")[0].files[0]);

            myform.append('fileType',$("#fileType").val());

            myform.append('keywords',$("#keywords").val());

            $.ajax({

                url: sy.managerServerUrl + "file/uploadFileByFrom",//"http://192.168.5.206:8983/solr/update/extract",

                type: "POST",

                data: myform,

                async: false,

                contentType: false,

                processData: false,

                success: function (result) {

                    if (result && result.result) {

                        getFileListPage();

                    } else {

                        $.messager.alert("警告", "文件上传失败!", "warning", function () {

                            var file = document.getElementById('file');

                            file.value = '';

                        });

                    }

                },

                error: function (data) {

                    $.messager.alert("警告", "文件上传失败!", "warning", function () {

                        var file = document.getElementById('file');

                        file.value = '';

                    });

                }

            });

        }

 

后台代码

 

 

/**
 * 单文件上传 指定 from 表單屬性 enctype="multipart/form-data" ,指定from表單中待提交文件name為 file
 * @param file
 * @return
 */
@ResponseBody
@RequestMapping("/uploadFileByFrom")
public MsgResult uploadFileByFrom(@RequestParam("file") MultipartFile file, String keywords, String fileType) {
    logger.info("upload file by form");
    MsgResult result = new MsgResultVO();
    if (!file.isEmpty()) {
        result = fileService.uploadFileByFrom(file);
    } else {
        result.errorResult(MsgConstantUtil.REQUEST_PARAMETER_ERROR);
    }
    return result;
}
展开阅读全文

没有更多推荐了,返回首页