VUE+axios+php實(shí)現(xiàn)圖片上傳
本文實(shí)例為大家分享了VUE+axios+php實(shí)現(xiàn)圖片上傳的具體代碼,供大家參考,具體內(nèi)容如下
前端部分
執(zhí)行函數(shù),注意file那有個(gè)await,獲取到文件數(shù)據(jù)后,裝在FormData里面,然后調(diào)用下文axios的uploadFile
async uploadFiles() { ? ? ? let inputObj = document.createElement("input"); ? ? ? let file = await new Promise((res) => { ? ? ? ? inputObj.setAttribute("id", "file"); ? ? ? ? inputObj.setAttribute("type", "file"); ? ? ? ? inputObj.setAttribute("name", "file"); ? ? ? ? inputObj.setAttribute("style", "visibility:hidden"); ? ? ? ? document.body.appendChild(inputObj); ? ? ? ? inputObj.click(); ? ? ? ? document.querySelector("#file").addEventListener("change", (e) => { ? ? ? ? ? console.log(e.target.files); ? ? ? ? ? res(e.target.files) ? ? ? ? }); ? ? ? }); ? ? ? let formdata = new FormData() ? ? ? formdata.append('myFile', file[0]) ? ? ? await this._http.uploadFile(formdata).then(console.log); ? ? ? document.body.removeChild(inputObj) ? ? },
http請(qǐng)求集中管理
async uploadFile(file) { ? ? return axios.post(`/Upload.php`,file,{ ? ? ? headers: {"Content-Type": "multipart/form-data"} ? ? }) ? }
php后端部分
接收參數(shù),調(diào)用uploadFile
<?php header('content-type:text/html;charset=utf-8'); $fileInfo=$_FILES["myFile"]; $maxSize=10485760;//10M,10*1024*1024 $allowExt=array('jpeg','jpg','png','tif'); $path="uploads"; include_once 'UploadFile.php'; uploadFile($fileInfo, $path, $allowExt, $maxSize);
封裝uploadFile函數(shù)
<?php function uploadFile($fileInfo, $path, $allowExt, $maxSize) { ? $filename = $fileInfo["name"]; ? $tmp_name = $fileInfo["tmp_name"]; ? $size = $fileInfo["size"]; ? $error = $fileInfo["error"]; ? $type = $fileInfo["type"]; ? //服務(wù)器端設(shè)定限制 ? $ext = pathinfo($filename, PATHINFO_EXTENSION); ? //目的信息 ? if (!file_exists($path)) { ? ? mkdir($path, 0777, true); ? ? chmod($path, 0777); ? } ? $uniName = md5(uniqid(microtime(true), true)) . '.' . $ext; ? $destination = $path . "/" . $uniName; ? if ($error == 0) { ? ? if ($size > $maxSize) { ? ? ? exit("上傳文件過大!"); ? ? } ? ? if (!in_array($ext, $allowExt)) { ? ? ? exit("非法文件類型"); ? ? } ? ? if (!is_uploaded_file($tmp_name)) { ? ? ? exit("上傳方式有誤,請(qǐng)使用post方式"); ? ? } ? ? //判斷是否為真實(shí)圖片(防止偽裝成圖片的病毒一類的 ? ? if (!getimagesize($tmp_name)) { //getimagesize真實(shí)返回?cái)?shù)組,否則返回false ? ? ? exit("不是真正的圖片類型"); ? ? } ? ? if (@move_uploaded_file($tmp_name, $destination)) { //@錯(cuò)誤抑制符,不讓用戶看到警告 ? ? ? echo "文件" . $filename . "上傳成功!"; ? ? } else { ? ? ? echo "文件" . $filename . "上傳失敗!"; ? ? } ? } else { ? ? switch ($error) { ? ? ? case 1: ? ? ? ? echo "超過了上傳文件的最大值,請(qǐng)上傳2M以下文件"; ? ? ? ? break; ? ? ? case 2: ? ? ? ? echo "上傳文件過多,請(qǐng)一次上傳20個(gè)及以下文件!"; ? ? ? ? break; ? ? ? case 3: ? ? ? ? echo "文件并未完全上傳,請(qǐng)?jiān)俅螄L試!"; ? ? ? ? break; ? ? ? case 4: ? ? ? ? echo "未選擇上傳文件!"; ? ? ? ? break; ? ? ? case 7: ? ? ? ? echo "沒有臨時(shí)文件夾"; ? ? ? ? break; ? ? } ? } ? return $destination; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled
最近在做項(xiàng)目的時(shí)候遇到了些問題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01Vue項(xiàng)目中引入外部文件的方法(css、js、less)
本篇文章主要介紹了Vue項(xiàng)目中引入外部文件的方法(css、js、less),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07使用Vue實(shí)現(xiàn)簡(jiǎn)單日歷效果
這篇文章主要為大家詳細(xì)介紹了使用Vue實(shí)現(xiàn)簡(jiǎn)單日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08vue項(xiàng)目開發(fā)中setTimeout等定時(shí)器的管理問題
這篇文章主要介紹了vue項(xiàng)目開發(fā)中setTimeout等定時(shí)器的管理問題,需要的朋友可以參考下2018-09-09Vue中使用回車鍵觸發(fā)事件的方法實(shí)現(xiàn)
本文主要介紹了Vue中使用回車鍵觸發(fā)事件的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新
這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-04-04