Vue+thinkphp5.1+axios實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了使用thinkphp5.1 + Vue+axios實(shí)現(xiàn)文件上傳,供大家參考,具體內(nèi)容如下
前言
使用thinkphp5.1 + Vue+axios+實(shí)現(xiàn)文件上傳
一、頁(yè)面代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上傳Demo</title>
<style>
.fileBtn{
width: 180px;
height: 36px;
line-height: 36px;
background: skyblue;
border-radius: 5px;
display: block;
text-align: center;
color: white;
}
[v-cloak] {
display: none;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1 v-cloak>{{message}}</h1>
<form>
<input type="file" name="file" ref="file" id="file" v-on:change="upload" style="visibility: hidden;" />
<label for="file" class="fileBtn">上傳</label>
</form>
</div>
</body>
</html>
<script>
var vue = new Vue({
el:'#app',
data:{
message:'文件上傳',
},
methods:{
upload:function(file) {
console.log(file.target.files[0]);
var forms = new FormData()
var configs = {
headers:{'Content-Type':'multipart/form-data;charse=UTF-8'}
};
forms.append('file',file.target.files[0]);
axios.post('http://127.0.0.1/index/index/upload', forms,configs)
.then(function (response) {
if (response.data.code == 0) {
alert('文件上傳成功');
} else {
alert('文件上傳失敗');
}
file.target.value = '';
})
.catch(function (error) {
console.log(error);
});
}
}
});
</script>

二、解決接口跨域問(wèn)題
這里使用的apache 2.4.8,找到httpd.conf ,添加一行配置:
Header set Access-Control-Allow-Origin *

三.后端處理上傳部分
/**
* 文件上傳方法校驗(yàn)
*/
public function upload()
{
try{
$file = request()->file('file');
if (empty($file)) {
echo json_encode(['code' => 1,"msg" => '請(qǐng)選擇上傳文件'],JSON_UNESCAPED_UNICODE);exit;
}
// 移動(dòng)到框架應(yīng)用根目錄/uploads/ 目錄下
$info = $file->move( '../uploads');
if($info){
// 成功上傳后 獲取上傳信息
// 輸出 jpg
echo json_encode(['code' => 0,"msg" => 'succcess'],JSON_UNESCAPED_UNICODE);exit;
}else{
// 上傳失敗獲取錯(cuò)誤信息
echo json_encode(['code' => 1,"msg" => 'error'],JSON_UNESCAPED_UNICODE);exit;
}
} catch (Exception $e) {
echo json_encode(['code' => 1,"msg" => 'error'],JSON_UNESCAPED_UNICODE);exit;
}
}
四.實(shí)際效果


測(cè)試成功!
關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue Router路由動(dòng)態(tài)緩存組件使用詳解
這篇文章主要介紹了Vue Router路由動(dòng)態(tài)緩存組件使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Vue 自定義指令實(shí)現(xiàn)一鍵 Copy功能
指令 (Directives) 是帶有 v- 前綴的特殊特性。這篇文章主要介紹了Vue 自定義指令實(shí)現(xiàn)一鍵 Copy的功能,需要的朋友可以參考下2019-09-09
Vue組件庫(kù)Element-常見(jiàn)組件表格示例代碼
對(duì)于Element組件的使用,最主要的就是明確自己想要達(dá)到的效果,從官網(wǎng)中將對(duì)應(yīng)代碼復(fù)制粘貼即可,最重要的是要讀懂不同組件官網(wǎng)中提供的文檔,以便實(shí)現(xiàn)自己想要的效果,本文給大家介紹Vue組件庫(kù)Element-常見(jiàn)組件表格,感興趣的朋友一起看看吧2023-10-10
Vue API中setup ref reactive函數(shù)使用教程
setup是用來(lái)寫(xiě)組合式api,內(nèi)部的數(shù)據(jù)和方法需要通過(guò)return之后,模板才能使用。在之前vue2中,data返回的數(shù)據(jù),可以直接進(jìn)行雙向綁定使用,如果我們把setup中數(shù)據(jù)類型直接雙向綁定,發(fā)現(xiàn)變量并不能實(shí)時(shí)響應(yīng)。接下來(lái)就詳細(xì)看看它們的使用2022-12-12

