Home  >  Article  >  Web Front-end  >  How layui implements image uploading and image preview (pure code)

How layui implements image uploading and image preview (pure code)

不言
不言Original
2018-08-13 17:27:0810238browse

The content of this article is about how layui implements image uploading and image preview (pure code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Automatically upload after selecting a file

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="../../layui/css/layui.css"/>
    </head>
    <body>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
          <legend>常规使用:普通图片上传</legend>
        </fieldset>
         
        <div class="layui-upload">
          <button type="button" class="layui-btn" id="test1">上传图片</button>
          <div class="layui-upload-list">
              <!--预览图片-->
            <img class="layui-upload-img" id="demo1">
            <!--提示上传信息-->
            <p id="demoText"></p>
          </div>
        </div>   
        
        <script type="text/javascript" src="../../layui/layui.js"></script>
        <script>
            layui.use([&#39;upload&#39;,&#39;jquery&#39;], function(){
                var $ = layui.$,
                upload = layui.upload;
                
                 //普通图片上传
                  var uploadInst = upload.render({
                    elem: &#39;#test1&#39;
                    ,url: &#39;/upload/&#39;
                    ,before: function(obj){//文件上传前的回调
                      //预读本地文件示例,不支持ie8
                      obj.preview(function(index, file, result){
                        $(&#39;#demo1&#39;).attr(&#39;src&#39;, result); //图片链接(base64)直接将图片地址赋值给img的src属性
                      });
                    }
                    ,done: function(res){
                      //如果上传失败
                      if(res.code > 0){
                        return layer.msg(&#39;上传失败&#39;);
                      }
                      //上传成功
                    }
                    ,error: function(){
                      //演示失败状态,并实现重传
                      var demoText = $(&#39;#demoText&#39;);
                      demoText.html(&#39;<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>&#39;);
                      demoText.find(&#39;.demo-reload&#39;).on(&#39;click&#39;, function(){
                        uploadInst.upload();
                      });
                    }
                  });
            });
        </script>
    </body>
</html>

Image preview implementation, the callback after selecting the file and the callback before uploading the image can be implemented:

obj.preview(function(index, file, result){
                        //index表示文件索引
                        //file表示文件信息
                        //result表示文件src地址
				        $(&#39;#demo1&#39;).attr(&#39;src&#39;, result); //图片链接(base64)直接将图片地址赋值给img的src属性
				      });

Click to upload the image After that, select the picture and upload it directly:

2. After selecting the file, it will not be uploaded automatically. The upload will be triggered by the button.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../../layui/css/layui.css"/>
	</head>
	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		  <legend>常规使用:普通图片上传</legend>
		</fieldset>
		 
		<p class="layui-upload">
		  <button type="button" class="layui-btn" id="choose">选择文件</button>
		  <p class="layui-upload-list">
		  	<!--预览图片-->
		    <img class="layui-upload-img" id="demo1">
		    <!--提示上传信息-->
		    <p id="demoText"></p>
		  </p>
		  <button type="button" class="layui-btn" id="load">上传</button>
		</p>   
		
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<script>
			layui.use([&#39;upload&#39;,&#39;jquery&#39;], function(){
				var $ = layui.$,
				upload = layui.upload;
				
				 //普通图片上传
				  var uploadInst = upload.render({
				    elem: &#39;#choose&#39;//选择文件的DOM对象
				    ,auto: false //选择文件后不自动上传
				    ,bindAction: &#39;#load&#39; //指向一个按钮触发上传
				    ,url: &#39;/upload/&#39;
				    ,choose:function(obj){//选择文件的回调,obj为选中的文件
				    	//将每次选择的文件追加到文件队列
    					var files = obj.pushFile();
    					
    					//预览选中的文件(本地文件)
    					obj.preview(function(index,file,result){
    						 $(&#39;#demo1&#39;).attr(&#39;src&#39;, result); 
    					});
				    }
				    ,before: function(obj){//文件上传前的回调
				      
				    }
				    ,done: function(res){
				      //如果上传失败
				      if(res.code > 0){
				        return layer.msg(&#39;上传失败&#39;);
				      }
				      //上传成功
				    }
				    ,error: function(){
				      //演示失败状态,并实现重传
				      var demoText = $(&#39;#demoText&#39;);
				      demoText.html(&#39;<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>&#39;);
				      demoText.find(&#39;.demo-reload&#39;).on(&#39;click&#39;, function(){
				        uploadInst.upload();
				      });
				    }
				  });
			});
		</script>
	</body>
</html>

After clicking to select the file, select the picture. , the selected picture can be displayed for preview, then click Upload to upload the picture:

##Related recommendations:

How to export and import excel using js? How to import and export excel using js (pure code)

How to move the drop-down menu left and right in js (code)

The above is the detailed content of How layui implements image uploading and image preview (pure code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn