zoukankan      html  css  js  c++  java
  • 通过jQuery Ajax使用FormData对象上传文件

    废话不说直接上代码

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<script src="jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
    		<style>
    			.fileImg {
    				position: relative;
    				box-sizing: border-box;
    				 80px;
    				height: 80px;
    				margin-top: 20px;
    			}
    			
    			.fileImg img {
    				 100%;
    				height: 100%;
    			}
    			
    			.delImg {
    				position: absolute;
    				right: -10px;
    				top: -10px;
    				 20px;
    				height: 20px;
    				background-color: #D9534F;
    				color: #FFFFFF;
    				-webkit-border-radius: 50%;
    				-moz-border-radius: 50%;
    				border-radius: 50%;
    				text-align: center;
    				font-size: 22px;
    				font-weight: bold;
    				line-height: 1;
    				cursor: pointer;
    			}
    		</style>
    		<script>
    			$(function() {
    				$("#file").on("change", function() {
    					var formData = new FormData();
    					formData.append('file', $('#file')[0].files[0]);
    					$.ajax({
    						url: '/upload',
    						type: 'POST',
    						cache: false,
    						data: formData,
    						processData: false,
    						contentType: false
    					}).done(function(res) {
    						//一般后台会返回个图片路径,然后将路径放到页面显示就可以了
    						
    					}).fail(function(res) {
    						
    					});
    
    				})
    			})
    		</script>
    	</head>
    
    	<body>
    
    		<div class="fileImg">
    			<img src="/img/2.png" />
    			<div class="delImg">×</div>
    		</div>
    		<div id="uploadForm">
    			<input id="file" type="file" />
    		</div>
    	</body>
    
    </html>
    

      

    参考链接    https://www.jianshu.com/p/46e6e03a0d53

  • 相关阅读:
    我該怎麼辦
    這麼多年興許從來沒有釋放過
    我想做一個快樂的孩子
    纠结
    如何克服心煩氣躁
    如何樹立威嚴
    第零次作业
    第三周作业
    第二周作业,
    第二周作业
  • 原文地址:https://www.cnblogs.com/qqing/p/8321716.html
Copyright © 2011-2022 走看看