js实现上传多个图片或者多个文件

主要是运用html5的    filereader可以参考  http://blog.csdn.net/yaoyuan_difang/article/details/38582697

FileReader的使用

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title> 上传多张图片</title>
    <style type="text/css">
        .img-div{
            border: 1px solid #ddd;
            border-radius: 100%;
            float: left;
            line-height: 1;
            margin-left: 5px;
            overflow: hidden;
        }

    </style>
</head>
<body>

<script type="text/javascript">
    //选择图片,马上预览
    function xmTanUploadImg(obj) {

        var fl=obj.files.length;
        for(var i=0;i<fl;i++){
            var file=obj.files[i];
            var reader = new FileReader();

            //读取文件过程方法

            reader.onloadstart = function (e) {
                console.log("开始读取....");
            }
            reader.onprogress = function (e) {
                console.log("正在读取中....");
            }
            reader.onabort = function (e) {
                console.log("中断读取....");
            }
            reader.onerror = function (e) {
                console.log("读取异常....");
            }
            reader.onload = function (e) {
                console.log("成功读取....");

                var imgstr='<img style="width:100px;height:100px;" src="'+e.target.result+'"/>';
                var oimgbox=document.getElementById("imgboxid");
                var ndiv=document.createElement("div");

                ndiv.innerHTML=imgstr;
                ndiv.className="img-div";
                oimgbox.appendChild(ndiv);
               
            }

            reader.readAsDataURL(file);
//alert(1);
        }

    }
</script>
<form id="form"  enctype="multipart/form-data">
		<input type="file" id="xdaTanFileImg"  multiple="multiple"  name="fileAttach" οnchange="xmTanUploadImg(this)"/>
    <div class="img-box" id="imgboxid">

    </div>

	                  <div id="xmTanDiv"></div><br/>
	            <div id="errordiv"   style="margin-top:15px;width:100%;text-align:center;">
	            <input id="bt" type="button" οnclick="test(this)" value="提交" /> 
            </div>
</form>
</body>

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
element-plus 是一款基于 Vue.js 的组件库,可以方便地实现多个图片上传功能。 首先,我们需要在项目中安装并引入 element-plus 组件库。可以通过 npm 或者 yarn 进行安装,然后在项目的主文件中引入 element-plus 的样式文件和所需的组件,例如: ```js import { ElUpload, ElButton } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; ``` 接下来,在 Vue 组件中使用 ElUpload 组件来实现图片上传。我们可以使用 v-for 指令循环渲染多个上传区域,每个区域都有一个独立的上传按钮和对应的图片显示区域。可以在模板中插入以下代码: ```html <template> <div> <div v-for="uploadItem in uploadItems" :key="uploadItem.id"> <el-upload class="upload" :action="uploadItem.action" :limit="uploadItem.limit" :on-success="handleUploadSuccess" > <el-button size="small" type="primary">选择图片</el-button> </el-upload> <div class="image-list"> <img v-for="url in uploadItem.imageList" :key="url" :src="url" /> </div> </div> </div> </template> ``` 在 data 中定义一个数组 uploadItems,用来存储每个上传区域的相关配置信息,包括上传地址、上传限制等。可以在 created 钩子函数中初始化 uploadItems 数组: ```js export default { data() { return { uploadItems: [ { id: 1, action: '/upload', limit: 3, imageList: [] }, { id: 2, action: '/upload', limit: 5, imageList: [] }, // 可以根据需要继续添加更多的上传区域配置 ] }; }, // ... } ``` 最后,实现 handleUploadSuccess 方法,在上传成功后将图片的 URL 添加到对应的 uploadItem.imageList 数组中: ```js export default { // ... methods: { handleUploadSuccess(response, file, uploadItem) { uploadItem.imageList.push(response.url); } } } ``` 通过以上方法,在页面中就可以实现多个图片上传功能了。每个上传区域都独立管理自己的图片列表,方便实现一次上传多个图片的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值