多張圖片上傳
<!DOCTYPE html>
<html>
<head>
<title>html5移動(dòng)端上傳本地圖片</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
<style type="text/css">
#test div{ width:200px; height:200px; display:inline-block; text-align:center;}
#test div img{ max-width:200px; max-height:200px;}
</style>
</head>
<body>
<input type="file" id="file_input" multiple />
<div id="test"></div>
<script>
window.onload = function(){
var input = document.getElementById('file_input');
var result, div;
if(typeof FileReader === 'undefined'){
result.innerHTML = "抱歉惊豺,你的瀏覽器不支持 FileReader";
input.setAttribute('disabled','disabled');
}else{
input.addEventListener('change', readFile, false);
}
function readFile(){
for(var i=0; i<this.files.length; i++){
if( !input['value'].match(/.jpg|.gif|.png|.bmp/) ){ //判斷上傳文件格式
return alert('上傳的圖片格式不正確')
}
var reader = new FileReader();
reader.readAsDataURL(this.files[i]);
reader.onload = function(e){
result = '![](' + this.result + ')'
div = document.createElement('div')
div.innerHTML += result
document.getElementById('test').appendChild(div)
}
}
}
}
</script>
</body>
</html>
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html
Flex 布局教程:實(shí)例篇
單張圖片上傳
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<title>意見(jiàn)反饋</title>
</head>
<style>
body{
background: #ccc;
}
.wrapper{
height:700px;
background:#F2F3F4;
width:540px;
margin:0 auto;
overflow:hidden;
}
input[type="file"]{
opacity: 0;
position:absolute;
width:100%;
height: 100%;
border:none
z-index:1;
left:0;
cursor:pointer;
}
.photo-container{
padding:4% 3% 2% 3%;
background: #FFFFFF;
font-size: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.pushPhoto{
width: 20%;
height: 100px;
margin:0 2%;
position: relative;
border:1px solid #dcdcdc;
font-size:50px;
text-align:center;
line-height:100px;
}
.pushPhoto>img{
position: absolute;
top: 0;
left: 0;
z-index: 1;
width:0%;
height: 0%;
}
.close-btn{
display: none;
position: absolute;
right: 0;
top: 0;
color: #555;
font-size: 0.8rem;
background:#dcdcdc;
padding: 5%;
height:20px;
width:20px;
line-height:20px;
z-index: 2;
cursor:pointer;
}
.pushPhoto{
background: url(../img/advice_pic_btn.png) no-repeat center center;
background-size: contain;
}
.top-container{
padding: 3%;
position: relative;
background: #FFFFFF;
}
.top-container:after,.top-container:before{
content: "";
position: absolute;
width: 200%;
height: 0;
left: -100%;
border-bottom: 1px solid #DCDCDC;
z-index: 1;
}
.top-container:before{
top: 0;
}
.top-container:after{
top: 100%;
}
.top-container textarea{
width: 100%;
height: 10rem;
resize: none;
outline: none;
font-size: 18px;
border: none;
overflow: scroll;
}
.photo-box{
background: #DFE1EF;
}
.photo-container>div{
display: inline-block;
}
.line-container{
background: #FFFFFF;
margin: 8% 0;
padding: 3%;
}
.line-container>input{
border:none;
width: 90%;
font-size: 18px;
}
.btn{
border:none;
display: block;
width: 95%;
margin:0% auto;
margin-top: 9%;
background: #368CFC;
color: #FFFFFF;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
padding: 3% 0;
font-size: 20px;
outline:none;
}
.btn:active{
background: #88bcff;
}
.photo-container .photo-tips{
display: block;
width: 20%;
margin:0 2.5%;
text-align: center;
font-size: 1.36rem;
color: #555;
padding:1% 0;
}
</style>
<body>
<div class="wrapper">
<form id="fm_feedBack" enctype="multipart/form-data" method="post">
<div class="top-container">
<textarea name="content" id="adviceContent" name="" rows="" cols="" placeholder="把你使用過(guò)程的感受圃泡、意見(jiàn)告訴我們吧普舆,我們會(huì)努力改進(jìn)的异袄!"></textarea>
</div>
<div class="photo-container">
<div class="pushPhoto" style="position: relative;">
+
<span class="close-btn" onclick="delImg(this)">X</span>
<img />
<input name="adviceImga" type="file" accept="image/*" value="+" capture="camera" onchange="imgPreview(this)">
</div>
<span class="photo-tips">上傳截圖</span>
</div>
<div class="line-container">
<input type="tel" name="phoneNo" id="contactMethod" value="" placeholder="請(qǐng)?zhí)顚?xiě)您的聯(lián)系方式"/>
</div>
<button type="button" class="btn" onclick="submitAddAdvice()">提交反饋</button>
</form>
</div>
</body>
<script type="text/javascript" charset="utf-8" src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script>
//上傳圖片
var pushPhotoLength = 4;
//圖片上傳框
var txt = '<div class="pushPhoto" style="position: relative;">+<span class="close-btn" onclick="delImg(this)">X</span><img /><input type="file" accept="image/*" value="+" capture="camera" onchange="imgPreview(this)"></div>';
var data = {"result":"意見(jiàn)提交成功","resultCode":1};
function imgPreview(fileDom){
//判斷是否支持FileReader
if (window.FileReader) {
var reader = new FileReader();
} else {
alert("您的設(shè)備不支持圖片預(yù)覽功能性誉,如需該功能請(qǐng)升級(jí)您的設(shè)備缴允!");
}
//獲取文件
var file = fileDom.files[0];
var imageType = /^image\//;
//是否是圖片
if (!imageType.test(file.type)) {
alert("請(qǐng)選擇圖片米同!");
return;
}
//讀取完成
reader.onload = function(e) {
//獲取圖片dom
var img = $(fileDom).prev();
var closeBtn = $(img).prev();
var pushPhoto = $(".pushPhoto");
$(img).attr("src",e.target.result);
$(img).css({
"width":"100%",
"height":"100%"
})
closeBtn.show();
if(pushPhoto.length != 4){
$(fileDom).parent().after(txt);
}else{
pushPhotoLength = 0;
}
};
reader.readAsDataURL(file);
}
//刪除上傳的圖片
function delImg(obj){
var pushPhoto = $(".pushPhoto");
if(pushPhoto.length == 4 && pushPhotoLength == 0){
$(obj).parent().remove();
pushPhoto = $(".pushPhoto");
$(pushPhoto[2]).after(txt);
pushPhotoLength = $(".pushPhoto").length;
}else if(pushPhoto.length == 1){
$(obj).hide();
$(obj).next("img").css("height","0");
}else{
$(obj).parent().remove();
}
}
//提交反饋
function submitAddAdvice(){
var phoneNo = $("#contactMethod").val();
var content = $("#adviceContent").val();
if(content =="" || content == null || content == undefined){
alert("請(qǐng)您先填寫(xiě)反饋意見(jiàn)");
return false;
}else if(phoneNo =="" || phoneNo == null || phoneNo == undefined){
alert("請(qǐng)您先填寫(xiě)聯(lián)系方式");
return false;
}else{
if(data.resultCode==1){
alert(data.result);
}
}
}
</script>
</html>
http://yesnbst.com/Arts/detail/artId/167使用AJAX上傳圖片至服務(wù)器的方法
<form action="#" id="testForm" method="post" enctype="multipart/form-data">
<input type="file" id="files" name="photo">
<button type="button" id="btn">確定</button>
</form>
$("#btn").on("click", function(){
var formData = new FormData($( "testForm" )[0]); // 使用FormData對(duì)象進(jìn)行上傳
formData.append('photo',$("#files")[0].files[0]);
$.ajax({
url: '{:U("Index/uploader")}' , // 服務(wù)器地址
type: 'POST',
data: formData, // 上傳的對(duì)象
dataType:'json',
async: false,
cache: false,
contentType: false,
processData: false,
success: function (response) {
console.log(response);
},
});
})
原文地址:
http://yesnbst.com/Arts/detail/artId/167使用AJAX上傳圖片至服務(wù)器的方法
http://www.qdfuns.com/notes/35528/603e005101b744ea58e52292449dbccd.html