js页面上传多个图片文件的功能

页面的:
<tr>
    <td style="width: 10%; vertical-align:middle;"><font
            color="red">*</font>上传
    </td>
    <td style="width: 90%; vertical-align:middle;" colspan="5">
        <div class="file-box-list cnsAttachInput"
             style="display: inline-block;margin: 0px"></div>
        <div style="display: inline-block" class="file-box">
            <input type="file" name="files" id="cnsAttachInput"
                   οnchange="UploadFile('cnsAttachInput','cnsAttach')"
                   style="opacity: 0;">
                <input type="hidden" id="cnsAttach" name="cnsAttach"
                    value="${ds.cnsAttach}">  //上传时候返回显示用
        </div>
    </td>
</tr>
js:UploadFile 方法:
function UploadFile(id, name) {
    var fileName = $("#" + id).val();
    if (fileName == null || fileName == "") {
        return false;
    }
    if (!(/\.(jpe?g|gif|png|ico)$/i).test(fileName)) {
        alert("请上传图片");
        $("#" + id).val("");
        return false;
    }
    $.ajaxFileUpload({
        url: window.apppath + "/admin/api/approval/induction/upload?path=attach&createThumbnail=true&thumbnailWidth=80&thumbnailHeight=80",
        secureuri: false,
        fileElementId: id,//file控件id
        dataType: 'json',
        success: function (data, status) {
            if (data.error == 0) {
                fileName = fileName.substr(fileName.lastIndexOf("\\") + 1, fileName.length - 1);
                var html = "";
                if (!(/\.(jpe?g|gif|png|ico)$/i).test(fileName)) {
                    html = "<label style='margin: 0px;    vertical-align: top'>" + fileName + "<a style='text-decoration: none' data='" + data.url + "' class='remove'>&times;</a></label>"
                }
                else {
                    var path = data.url.substr(0, data.url.lastIndexOf(".")) + "_80_80" + data.url.substr(data.url.lastIndexOf("."))
                    html = "<div class='file-div''><a target='_blank' href='" + window.apppath + data.url + "'><img class='uploadImg' src='" + window.apppath + path + "'></a><a style='text-decoration: none;display: none' data='" + data.url + "' class=' remove removeImg'></a></div>"
                }
                $("." + id + "").append(html);
                $(".remove").click(function () {
                    var url = $(this).attr("data");
                    $(this).parent().remove();
                    var urls = $("input[name=" + name + "]").val();
                    urls = urls.replace(url + ",", "")
                    $("input[name=" + name + "]").val(urls);
                });
                $(".uploadImg").hover(function () {
                    $(this).parent().parent().find(".remove").css("display", "inherit");
                })
                $(".remove").hover(function () {
                }, function () {
                    $(this).hide();
                })
                $("#" + id).val("");
                $("input[name=" + name + "]").val($("input[name=" + name + "]").val() + data.url + ",");
            }
        }
    });
后台控制类里upload:

@RequestMapping(value = "upload")
@ResponseBody
public UploadResult uploadFile(HttpServletRequest request, @RequestParam("files") MultipartFile files, @RequestParam("path") String path,
                               @RequestParam(value = "createThumbnail", defaultValue = "false", required = false) boolean createThumbnail,
                               @RequestParam(value = "thumbnailWidth", defaultValue = "200", required = false) Integer thumbnailWidth,
                               @RequestParam(value = "thumbnailHeight", defaultValue = "300", required = false) Integer thumbnailHeight) {
    UploadResult res = new UploadResult();
    FileUtil util = new FileUtil();
    JsonResult jsonResult = util.saveFile(request, files, "/file/approval/induction/" + path + "/", createThumbnail, thumbnailWidth, thumbnailHeight);
    if (jsonResult.isSuccess()) {
        res.markSuccess(jsonResult.getData().toString());
    } else {
        res.markError(jsonResult.getMessage());
    }
    return res;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值