微信小程序上傳圖片到別的域名文件下的示例代碼
效果

wxml
<!-- 上傳照片 -->
<view class="addbtn">
<view class='pic' name="fault_photo" wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
<image class='weui-uploader_img ' src="{{item}}" data-index="{{index}}" mode="aspectFill" bindtap="previewImg">
<image src='{{clear}}' class="delete-btn" data-index="{{index}}" catchtap="deleteImg"></image>
</image>
</view>
<view class="addphoto" bindtap="add_photo">
<image src="{{add}}" class="addtext">+</image>
</view>
</view>
<button bindtap="sumbit">提交</button>wxss
/* 上傳照片樣式 */
.line3 {
padding-top: 8%;
background-color: white;
width: 100%;
padding-bottom: 4%;
/* border: 1px solid black; */
}
.addbtn {
padding-top: 5%;
margin-left: 2%;
padding-bottom: 5%;
/* border: 1px solid black; */
}
.pic {
float: left;
position: relative;
margin-right: 9px;
margin-bottom: 9px;
/* border: 1px solid black; */
}
.weui-uploader_img {
/* border: 1px solid black; */
width: 150rpx;
height: 150rpx;
}
.delete-btn {
position: absolute;
top: -14rpx;
right: -14rpx;
width: 20px;
height: 20px;
z-index: 9999;
}
.addphoto {
width: 150rpx;
height: 150rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* background-color: #F6F6F6; */
border: 1px dashed #C6C6C6;
}
.addtext {
/* border: 1px solid black; */
/* font-size: 50px; */
width: 80rpx;
height: 80rpx;
color: #BFC1C2;
}js
const app = getApp()
Page({
data: {
//上傳照片圖片
clear: app.globalData.icon + 'photo/clear.png',
add: app.globalData.icon + 'photo/photo.png',
imgs: [],
allphoto: [],
},
//上傳圖片
add_photo(e) {
var that = this;
var imgs = this.data.imgs;
if (imgs.length >= 9) {
this.setData({
lenMore: 1
});
setTimeout(function () {
that.setData({
lenMore: 0
});
}, 2500);
return false;
}
wx.chooseImage({ //圖片相機(jī)的選擇chooseMedia
// count: 1, // 默認(rèn)9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機(jī),默認(rèn)二者都有
success: function (res) {
// 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片
var tempFilePaths = res.tempFilePaths;
// console.log('返回圖片路徑信息', res.tempFilePaths)
var imgs = that.data.imgs;
for (var i = 0; i < tempFilePaths.length; i++) {
if (imgs.length >= 9) {
that.setData({
imgs: imgs
});
return false;
} else {
imgs.push(tempFilePaths[i]);
}
}
that.setData({
imgs: imgs
});
console.log('圖片合集', that.data.imgs);
}
});
},
// 刪除圖片
deleteImg: function (e) {
var imgs = this.data.imgs;
var index = e.currentTarget.dataset.index;
imgs.splice(index, 1);
this.setData({
imgs: imgs
});
// console.log('上傳圖片合集', this.data.imgs);
},
// 預(yù)覽圖片
previewImg: function (e) {
//獲取當(dāng)前圖片的下標(biāo)
var index = e.currentTarget.dataset.index;
//所有圖片
var imgs = this.data.imgs;
wx.previewImage({
//當(dāng)前顯示圖片
current: imgs[index],
//所有圖片
urls: imgs
})
},
sumbit(){
console.log(this.data.imgs)
//先執(zhí)行圖片上傳
let imgs = this.data.imgs
// console.log(this.data.imgs)
for (var i = 0; i < this.data.imgs.length; i++) {
var that = this
wx.uploadFile({
//別的域名文件
url: 'https://域名/api/api_wxmini.php',
filePath: imgs[i],
name: "file",
header: {
"content-type": "multipart/form-data"
},
success: function (res) {
if (res.statusCode == 200) {
wx.showToast({
title: "上傳成功",
icon: "none",
duration: 1500
})
console.log(res.data)
that.data.allphoto.push(res.data) //向數(shù)組末端插入數(shù)據(jù)
}
},
fail: function (err) {
wx.showToast({
title: "上傳失敗",
icon: "none",
duration: 2000
})
},
})
}
}
})php
別的文件路徑

圖片路徑

代碼
<?php
$file = $_FILES['file']; //獲取小程序傳來的圖片
$imgdirs = "../update_img/";//文件夾名稱(/upload/update_img/)
mkdirs($imgdirs);//創(chuàng)建$imgdirs文件夾
//獲取圖片文件的名字
$fileName = $_FILES["file"]["name"];
// //獲取圖片類型
$file_type = $_FILES["file"]["type"];
$type = '';
//判斷是否是圖片
switch ($file_type) {
case 'image/png':
$type = '.png';
break;
case 'image/gif':
$type = '.gif';
break;
case 'image/jpeg':
$type = '.jpg';
break;
}
//圖片保存的路徑
$savepath = $imgdirs.$fileName; //upload/update_img/文件名
// 臨時(shí)文件移動到指定文件夾
$rs = move_uploaded_file($_FILES["file"]["tmp_name"],$savepath);
//成功上傳文件
if($rs) {
$url = 'SO/'.$fileName;
echo json_encode($url, JSON_UNESCAPED_SLASHES);
}
else {
$result=array('errno'=>1,'message'=>'失敗信息');
echo json_encode($result);
}
//創(chuàng)建文件夾 權(quán)限問題
function mkdirs($dir, $mode = 0777){
if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
if (!mkdirs(dirname($dir), $mode)) return FALSE;
return @mkdir($dir, $mode);
}
?>微信公眾平臺
上添加需要訪問的域名

到此這篇關(guān)于微信小程序上傳圖片到別的域名文件下的文章就介紹到這了,更多相關(guān)微信小程序上傳圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS點(diǎn)擊圖片彈出文件選擇框并覆蓋原圖功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了JS點(diǎn)擊圖片彈出文件選擇框并覆蓋原圖功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08
使用window.print()前端實(shí)現(xiàn)網(wǎng)頁打印超詳細(xì)教程(含代碼示例)
前端實(shí)現(xiàn)打印功能的方法有很多,大家在網(wǎng)上隨便一搜就是一大堆,下面這篇文章主要給大家介紹了關(guān)于使用window.print()前端實(shí)現(xiàn)網(wǎng)頁打印的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
ES6 Generator函數(shù)的應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 Generator函數(shù)的應(yīng)用,結(jié)合實(shí)例形式分析了ES6 Generator函數(shù)異步操作與異常捕獲相關(guān)使用技巧,需要的朋友可以參考下2019-06-06
Firefox中通過JavaScript復(fù)制數(shù)據(jù)到剪貼板(Copy to Clipboard 跨瀏覽器版)
這篇文章主要介紹了irefox中通過JavaScript復(fù)制數(shù)據(jù)到剪貼板的方法,可以跨瀏覽器使用,大家可以使用看看2013-11-11
解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法
這篇文章主要為大家詳細(xì)介紹了解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法,需要的朋友可以參考下2016-07-07
JavaScript隊(duì)列數(shù)據(jù)結(jié)構(gòu)詳解
這篇文章主要介紹了JavaScript隊(duì)列數(shù)據(jù)結(jié)構(gòu)詳解,隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),隊(duì)列中允許兩種基礎(chǔ)操作,也就是插入和刪除,也就是入隊(duì)和出隊(duì)2022-07-07
JS實(shí)現(xiàn)的RGB網(wǎng)頁顏色在線取色器完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)的RGB網(wǎng)頁顏色在線取色器,結(jié)合完整實(shí)例形式分析了基于JS運(yùn)算及鼠標(biāo)事件響應(yīng)來操作頁面元素實(shí)現(xiàn)取色器功能的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12

