在上传文件的同时 传递参数 (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;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值