vue實(shí)現(xiàn)圖片上傳到后臺(tái)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)圖片上傳到后臺(tái)的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery-1.11.3.min.js"></script>
<style>
.upload {
margin: 30px 40px 0;
width: 122px;
height: 122px;
padding-bottom: 40px;
position: relative;
float: left;
}
.upload .btn {
position: absolute;
left: 20px;
bottom: 0;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
color: #Fff;
border-radius: 5px;
background: #ff6c00;
}
.upload .btn .file {
display: inline-block;
position: absolute;
width: 80px;
height: 30px;
left: 0;
top: 0;
opacity: 0;
}
.upload .btn .add{
position: absolute;
left: 0;
top: 0;
width: 80px;
height: 30px;
text-align: center;
}
.upload .imgs {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
border: 1px solid #ccc;
padding: 10px;
}
.upload .imgs img {
width: 100px;
height: 100px;
border: 1px solid #f1f1f1;
}
.upload .imgs .look {
position: absolute;
left: 10px;
top: 10px;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
background: rgba(0, 0, 0, 0.3);
color: #fff;
}
</style>
</head>
<body>
<!-- 模態(tài)框 -->
<div class="madel-img"></div>
<div class="upload_wrap clearfix">
<div class="upload upload1 fl">
<div class="btn">
<span class="add">上傳文件</span>
<input type="file" class="file" multiple>
<input type="hidden" class="imgUrl">
</div>
<div class="imgs">
<img src="shenfenzheng.jpg" alt="">
<div class="look">圖片示范</div>
</div>
</div>
</div>
<script>
;(function($){
/* 上傳圖片 */
$.fn.upload = function(id){
this.id = id;
this.img = this.id.find($(".imgs img"));
this.img_src = this.id.find($(".imgs img")).attr("src");
this.file = this.id.find($(".file"));
this.look = this.id.find($(".look"));
this.imgUrl = this.id.find($(".imgUrl"));
var that = this;
this.file.on("change",function(){
var files = this.files;//獲得上傳的所有圖片
var reader = new FileReader();//讀取本地圖片并顯示
var name = files[0].name;
var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
if(fileName !="jpg" && fileName !="jpeg" && fileName !="png" && fileName !="bmp"){
layer.msg("圖片格式不正確!");
that.img.attr("src",that.img_src)
return;
}
var fileSize = files[0].size;
var size = fileSize / 1024;
if(size>10000){
layer.msg("圖片不能大于10M");
return;
}else if(size <= 0){
layer.msg("圖片不能小于0M");
return;
}
reader.readAsDataURL(files[0]);//讀取第一張圖片的地址
//數(shù)據(jù)讀取完成后改變src地址
reader.onload = function(){
that.look.css({"display":"none"});
var image = new Image();//本地緩存一張圖片
var imgCur_src = this.result;//獲取正在上傳的圖片
that.img.attr("src",imgCur_src);//吧剛開(kāi)始的圖片替換成上傳的圖片
}
var fd = new FormData();
fd.append("pic", files[0]);
$.ajax({
type: "POST",
contentType: false, //必須false才會(huì)避開(kāi)jQuery對(duì) formdata 的默認(rèn)處理 , XMLHttpRequest會(huì)對(duì) formdata 進(jìn)行正確的處理
processData: false, //必須false才會(huì)自動(dòng)加上正確的Content-Type
url: uploadUrl,
data: fd,
async: false,
beforeSend: function (request) {
request.setRequestHeader("Authorization", localStorage.token);
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
},
success: function (msg) {
console.log(msg)
if (msg.code == "100") {
imgUrl.val(msg.data);
}
},
error: function (msg) {
console.log(msg);
}
});
})
}
$(".upload1").upload($(".upload1"));
})(jQuery)
</script>
</body>
</html>
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Vue實(shí)現(xiàn)圖片上傳的三種方式
- Vue.js 2.0 移動(dòng)端拍照壓縮圖片上傳預(yù)覽功能
- vue.js 圖片上傳并預(yù)覽及圖片更換功能的實(shí)現(xiàn)代碼
- vue+elementUI實(shí)現(xiàn)表單和圖片上傳及驗(yàn)證功能示例
- Vue2.0實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能 exif.js實(shí)現(xiàn)圖片上傳功能
- vue-quill-editor實(shí)現(xiàn)圖片上傳功能
- Vue2.0 實(shí)現(xiàn)移動(dòng)端圖片上傳功能
- vue+elementUI實(shí)現(xiàn)圖片上傳功能
- 基于vue+ bootstrap實(shí)現(xiàn)圖片上傳圖片展示功能
- 基于vue-upload-component封裝一個(gè)圖片上傳組件的示例
相關(guān)文章
vue3在table里使用elementUI的form表單驗(yàn)證的示例代碼
這篇文章主要介紹了vue3在table里使用elementUI的form表單驗(yàn)證的示例代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
vue大文件分片上傳之simple-uploader.js的使用
本文主要介紹了vue大文件分片上傳之simple-uploader.js的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作
這篇文章主要介紹了vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
通過(guò)vue方式實(shí)現(xiàn)二維碼掃碼功能
這篇文章給大家介紹了通過(guò)vue的方式,實(shí)現(xiàn)掃碼功能,實(shí)現(xiàn)步驟分為兩步,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2021-11-11
VueCli4項(xiàng)目配置反向代理proxy的方法步驟
這篇文章主要介紹了VueCli4項(xiàng)目配置反向代理proxy的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
利用vue組件自定義v-model實(shí)現(xiàn)一個(gè)Tab組件方法示例
這篇文章主要給大家介紹了關(guān)于利用vue組件自定義v-model實(shí)現(xiàn)一個(gè)Tab組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
el-tree?loadNode懶加載的實(shí)現(xiàn)
本文主要介紹了el-tree?loadNode懶加載的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

