前端上传多个图片(实现了拖拽图片进行交换图片)

在前端页面的form表单中写如下代码:
注:里面有一张add.png的图片,可以找适当的图片替换

 <div class="layui-form-item" style="margin-top: 15px;">
            <label class="layui-form-label" style="margin-left: 20px;">轮播图:</label>
            <div class="imgcon1" style="float: left;margin-left: 16px;"></div>
            <input id="videoImage1" name="lunbo" type="hidden" value="">
            <div class="addimg1" style="float: left;margin-left: 16px;">
                <div class="" id="drag1" title="将文件拖拽到此处上传">
                    <label for="fileupload1" title="点击上传">
                        <img alt="" src style="height: 100px;width: 100px;" th:src="@{/xadmin/images/add.png}">
                    </label>
                </div>
                <input accept="image/*" data-url="/recycle/goods/uploadImg" id="fileupload1" multiple="multiple" name="imageFile"
                       style="display: none" type="file">
            </div>
        </div>

注意这句:

<input accept="image/*" data-url="/recycle/goods/uploadImg" id="fileupload1" multiple="multiple" name="imageFile"
                       style="display: none" type="file">

其中的data-url是一个访问地址,在下面的controller中会写到
<script>标签中写上如下代码:

<!--拖动图片交换位置-->
<script>
    function allowDrop(ev)
    {
        ev.preventDefault();
    }

    var srcdiv = null;
    function drag(ev,divdom)
    {
        srcdiv=divdom;
        ev.dataTransfer.setData("text/html",divdom.innerHTML);
    }

    function drop(ev,divdom)
    {
        ev.preventDefault();
        if(srcdiv != divdom){
            srcdiv.innerHTML = divdom.innerHTML;
            divdom.innerHTML=ev.dataTransfer.getData("text/html");
        }
    }
</script>
<script>

    //删除图片
    function closeImg1(obj) {
        $(obj).parent().remove();
        // var goods_img = '';
        // $('.imgcon .img_div').each(function () {
        //     goods_img += $(this).find('img').attr('src') + ',';
        // })
        $('#videoImage1').val('');

        if ($('.imgcon1').children().length == 1) {
            $('.addimg1').css('display', 'none');
        }
        if ($('.imgcon1').children().length < 1) {
            $('.addimg1').css('display', 'inline-block');
        }
    }

    //上传图片
    $(function () {
        $('#fileupload1').fileupload({
            dataType: 'json',
            dropZone: $('#drag1'),
            change: function (e, data) {
                var len = $('.imgcon1').children().length;
                // if (data.files.length > (3 - parseInt(len))) {
                //     layer.msg('最多上传3张图片');
                //     return false;
                // }
            },
            start: function (e) {
                layer_msg = layer.msg('正在上传中…', {time: 100000000});
            },
            progressall: function (e, data) {
                $('.layui-layer-msg .layui-layer-content').html('已上传' + (data.loaded / data.total * 100).toFixed(2) + '%');
            },
            done: function (e, data) {
                layer.close(layer_msg);
                callback(data.result.msg);
            }
        });
        $('#drag1').bind('drop dragover', function (e) {
            e.preventDefault();
        }).on('dragenter', function (e) {
            $(this).addClass('dragenter');
        }).on('drop', function (e) {
            $(this).removeClass('dragenter');
        }).on('dragleave', function (e) {
            $(this).removeClass('dragenter');
        });

        /**
         * 数据回调
         * @param id
         * @param value
         */
        function callback(value) {
            var $li = $(
                "<div class=\"img_div\" style=\"position: relative;float:left;margin-right: 10px;\" οndrοp="drop(event,this)" οndragοver="allowDrop(event)" draggable="true" οndragstart="drag(event, this)">" +
                "<a οnclick=\"$(this).attr('href', $(this).find('img').attr('src'))\" type=\"button\"  data-lightbox=\"preview\">" +
                "<img src=\"" + value + "\" alt=\"\" style=\"width: 100px;\">" +
                "<input class='videoImage1' type='hidden' value=\"" + value + "\" name='lunbotu'>" +
                "</a>" +
                "<i class='layui-icon layui-icon-delete close' οnclick='closeImg1(this)' " +
                "style=\"position: absolute;top: -11px;right: -11px;color: red;font-weight: bolder;font-size: 22px;\"></i>" +
                "</div>"
            );

            $('.imgcon1').append($li);
            console.log($("#videoImage1"))
            $("#videoImage1").val(value);
            if ($('.imgcon1').children().length == 1) {
                $('.addimg1').css('display', 'none');
            }
            // var goods_img = '';
            // $('.imgcon .img_div').each(function () {
            //     goods_img += $(this).find('img').attr('src') + ',';
            // })
            $('.goods_img1').val($(this).find('img').attr('src'));
        }

        //图片回显
        if ("$document.getElementsByName('name')" != '') {
            var goods_img = $(document.getElementsByName('name'));
            $('.goods_img1').val(goods_img);
            // var arr = goods_img.split(',');
            // for (var i = 0; i < arr.length; i++) {
            //     if (arr[i] != '') {
            //         callback(arr[i]);
            //     }
            // }
            if ($('.imgcon1').children().length == 1) {
                $('.addimg1').css('display', 'none');
            }

        }
    });
    </script>

注意在前端页面中引如如下的js文件:

<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/vendor/jquery.ui.widget.js}" src="static/xadmin/lib/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"></script>
<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/jquery.iframe-transport.js}" src="static/xadmin/lib/jQuery-File-Upload/js/jquery.iframe-transport.js"></script>
<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/jquery.fileupload.js}" src="/xadmin/lib/jQuery-File-Upload/js/jquery.fileupload.js"></script>

这个就是上面提到的data-url的访问地址了:

@Controller
@RequestMapping("/recycle/goods")
public class GoodsController extends BaseController  {
	/**
     * 添加页面图片保存跳转
     */

    String path = "";

    @PostMapping("uploadImg")
    @ResponseBody
    public AjaxResult addFile(@RequestParam("imageFile") MultipartFile imageFile) {
        path = OssFileUtils.uploadSingleFile(imageFile);
        return success(path);
    }
	    /**
	* 添加
	* @param goods
	* @return
	*/
	@PostMapping("add")
	@ResponseBody
	@NoRepeatSubmit
	public AjaxResult add(String lunbotu){
		boolean  flag= goodsService.add(lunbotu);
		return toAjax(flag);
	}
}

上面写的add方法中,是在假设我的数据库中只有两个字段的情况下,一个是自增的id,另一个就是轮播图的阿里云地址。
add方法如下:

/**
     * 多图片上传
     */
    @Override
    public boolean add(String lunbotu) {
        //处理轮播图
        if (StringUtils.isNoneBlank(lunbotu)) {
            String[] imgs = lunbotu.split(",");
            for (String s : imgs) {
                goodsMapper.insertIntoLinks(s);
            }
            return true ;
        }
        return false ;
    }

这样就可以把多张图片都存到数据库里了。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常漂亮的100个 jquery前端案例,包含幻灯片切换、图片缩放、相册、放大镜、图片拖着滚动等,可以做为你的学习设计参考。 jquery+css五屏焦点图淡入淡出+圆形按钮切换广告图片代码 jQuery+CSS实用图片收缩与放大效果插件 jquery+div实现同时滑动切换的图文展示特效插件下载 jquery+div带动画按钮图片手动与自动切换的特效代码 jquery一页多用的飞飞图片幻灯插件演示 jquery仿flash产品图片多角度展示特效代码 jquery仿flash图片放大相册的插件代码(Zoomer Gallery)下载 jquery仿flash的图片幻灯片播放特效实例完整版 jquery仿LightBox图片盒子单击逐渐放大的图片展示的插件下载 jquery仿Lightbox的插件jQuery UI.ariaLightbox点击图片放大显示插件下载 jQuery仿动感flash自动滚动图片切换广告插件 jQuery仿新浪新闻图片浏览器(支持鼠标左右滚动控制图片切换) jquery仿苏宁易购商城产品图片全方位展示功能 jquery制作漂亮按钮示例打包 jQuery动态切换网页背景的大块图片的导航栏代码 jquery图文排行混合互相切换效果(仿Vancl效果) jQuery图片AnimatedPortfolioGallery jQuery图片AnimatedTextIconMenu jQuery图片BubblerifficImageGallery jQuery图片上下文滑动特效 jQuery图片插件之鼠标放在图片上出现动态的hover效果插件 jQuery图片特效404 jQuery图片特效CustomAnimationBanner jQuery图片特效FullPageImageGallery jQuery图片特效image Scroller图片滚动 jQuery图片特效ImageWall图片墙 jQuery图片特效interactive_picture jQuery图片特效jquery-twitter-ticker jQuery图片特效LatestPostSlider jQuery图片特效note-app jQuery图片特效panning-slideshow jQuery图片特效photo-shoot-css-jquery jQuery图片特效randomizer jQuery图片特效rotating-slideshow jQuery图片特效select-jquery jQuery图片特效Site jQuery图片特效SliderGallery jQuery图片特效smooth-rotatable-images-css3-jquery jQuery图片特效tz-todoapp jQuery图片特效youtube-player jQuery图片自动轮播淡入淡出的幻灯插件iFadeSlide jQuery大气banner带时间线自动播放的焦点图轮番切换代码 jquery定时自动切换banner广告图片动画插件示例 jquery实现LightBox图片点击放大效果的图片盒子插件 jQuery实现slider图片滚动,单个滚动,成组滚动示例 jQuery实现产品图片循坏旋转的代码 jQuery实现动态图文分组排序切换源码 jQuery实现图片3D旋转特效插件 v1.1版本下载 jQuery实现图片3D立体感的前后轮番展示特效 jQuery实现图片取景器仿相机拍照功能的插件photoShoot jQuery实现图片变色特效插件与实例下载如jquery图片变灰色 jquery实现图片可拖动展示的实例下载 jQuery实现拖动滚动条的缩略图排列插件下载 jQuery实现焦点图片Flash自动平滑渐变效果 jQuery实现鼠标移到链接提示显示图片功能插件 jquery实现鼠标经过链接放大图片特效代码 jquery实用Banner大图片横向切换效果 jquery实用产品图片展示动感切换效果源码 jquery平滑交换真彩色的图片逐渐变为黑白图像的代码 jquery异步加载图片的插件jqGalScroll下载 jquery微型相册插件Micro Image Gallery下载 jQuery把图片放大及变亮特效插件下载 jquery拖动滚动条控制图片滚动及图片放大特效的示例 jquery旋转式图片切换并带图片放大功能 jQuery漂亮网页右上角双层撕角广告代码 jquery漂亮网页布局综合定时器、切换间隔、滚动时间、滚动图片个数等示例 jQuery演示Ajax加载并显示图片的相片画廊实例 jQuery版Sexy Lightbox 2.3内容修饰框插件下载(支持HTML,flash,图片,视频等) jquery版自动滚动图片动画特效插件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值