最近学习小程序编程,上传图片到服务器一直不成功,网上查了不少资料方知是少了服务器端的接收代码。现在把代码贴出来供大家参考:
upanddown.wxml文件:

<!--pages/upanddown/upanddown.wxml-->
<view class="FileUploadContainer"><view class="title">Upload File</view><button bindtap="click">点击访问</button><!--文件上传--><view class="demo-box"><image wx:if="{{src}}" src="{{src}}" mode="aspectFit"></image><button bindtap="chooseImage">To choose File</button><button type="primary" bindtap="UploadFile">Upload</button></view><!--文件下载--><view><button type="primary" bindtap="DownloadFile">Download</button><image mode="aspectFit" wx:if="{{img}}" src="{{img}}" bindtap="previewImage"></image><button type="primary" bindtap="reset">Reset</button></view>
</view>

upanddown.js文件:

// pages/upanddown/upanddown.js
Page({/*** 页面的初始数据*/data: {src:'',//上传图片路径img:''},/*** 查看是否能连接到服务器*/click:function(){wx.request({url: 'http://localhostt/',data:{message:"message from wechat"},method:'GET',success:(res)=>{if(res.statusCode==200){console.log(res.data)}if(res.statusCode==404){wx.showToast({title: '找不到请求对象',icon:'none'})console.log("找不到请求对象")}},fail:(res)=>{wx.showToast({title: '找不到请求对象',icon:'none'})console.log("请求失败")}})},chooseImage:function(){var that=this;wx.chooseImage({count: 1,sizeType: ['original','compressed'],//指定是原图还是压缩图,默认二者都有sourceType: ['album','camera'],//指定是相册还是相机,默认二者都有success:(res)=>{let src=res.tempFilePaths[0];console.log(src);that.setData({src:src})}})},UploadFile:function(){var that=this;let src=this.data.src;console.log(src);if(src==''){wx.showToast({title: 'Plase choose file',icon:'none'})}else{var uploadTask=wx.uploadFile({filePath: src,name: 'file',url: 'http://localhost/imgupload.php',formData:{'filename':'fmw081418108.jpg'},success:function(res){console.log('[上传文件] 成功:', res)wx.showToast({title: res.data,})},fail: function (res) {console.log('上传失败');}})uploadTask.onProgressUpdate((res)=>{console.log('上传进度',res.progress)console.log('已经上传的数据长度',res.totalBytesSent)console.log('与其需要上传的数据总长度',res.totalBytesExpectedToSend)})}},previewImage:function(){wx.previewImage({current:this.data.img,urls:this.data.img})},DownloadFile:function(){var that=this;var filename="3.png";var downloadTask = wx.downloadFile({url: 'http://localhost/3.png',success:(res)=>{if(res.statusCode==200){console.log("下载成功");wx.playVoice({filePath: res.tempFilePath});let img=res.tempFilePath;console.log(res);that.setData({img:img})}else{console.log("下载失败",res.statusCode);}}})downloadTask.onProgressUpdate((res) => {console.log('下载进度', res.progress)console.log('已经下载的数据长度', res.totalBytesWritten)console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)})},
})

服务器端的imgupload.php文件

<?php$imgurl="https://localhost/";// 允许上传的图片后缀$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]);// echo $_FILES["file"]["size"];$extension = end($temp);     // 获取文件后缀名if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/x-png")|| ($_FILES["file"]["type"] == "image/png"))&& ($_FILES["file"]["size"] < 20480000)   // 小于 20M,这个自己限制&& in_array($extension, $allowedExts)){$imgpath=$_GET['imgpath'];  //获取传来的图片分类,用于在服务器上分类存放$code = $_FILES['file'];//获取小程序传来的图片$uploaded_file=$_FILES['file']['tmp_name'];$user_path=$_SERVER['DOCUMENT_ROOT'].$imgpath;  //放到服务器下指定的文件夹if(file_exists($user_path)){}else{mkdir($user_path,0777);}$size=$_FILES["file"]["size"];$date=date('Ymd'); //得到当前时间$newfilename='3.'.$extension; //得到一个新的文件名,可根据自己需求设定,sham用的时间加上图片文件大小来命名$move_to_file=$user_path."/".$newfilename;$file_true_name=$imgurl.$imgpath."/".$newfilename;//echo $file_true_name;$filename = json_encode($file_true_name);//把数据转换为JSON数据.// echo $move_to_file;move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file));//下面的代码是用来生成缩略图的$thump = $user_path."/thumb/";   //这个缩略图文件夹地址自己设置,这个是在原图文件夹里面增加1个子目录thumb用于存放缩略图if(file_exists($thump)){}else{mkdir($thump,0777);}$imgs = $newfilename;$imgss=$user_path."/".$imgs;$img_arr = getimagesize($imgss);$pecent = $img_arr[0]/$img_arr[1];$width = 200;    //这里是缩略图的尺寸,自行设置$height = 200/$pecent;//下面是根据不同图片后缀,执行不同的图片生成代码if($_FILES["file"]["type"] == "image/png"){$img_in = imagecreatefrompng($imgss);}elseif($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg"){$img_in = imagecreatefromjpeg($imgss);}elseif($_FILES["file"]["type"] == "image/gif"){$img_in = imagecreatefromgif($imgss);}$img_out = imagecreatetruecolor($width, $height);imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $width, $height, $img_arr[0], $img_arr[1]);imagejpeg($img_out,$thump.$imgs,100);imagedestroy($img_out);imagedestroy($img_in);//这里最后输出缩略图的网址,让小程序读取到,用于放入input用来传到数据库中echo $imgurl.$imgpath."/thumb/".$newfilename;}else{echo "上传错误";}?>

关于小程序上传图片到服务器相关推荐

  1. 微信小程序上传图片至服务器Springboot接收格式的问题

    微信小程序上传图片至服务器Springboot 需求:通过微信小程序上传图片到服务器,保存至服务器. 实现 Wxml <button bindtap="choose"> ...

  2. 微信小程序上传图片到服务器总是失败_微信小程序怎么上传图片到服务器?

    微信小程序怎么上传图片到服务器?相信很多人都会把小程序图片保存到本地吧,但是把图片上传到服务器就不一定了,下面一起随小编看看微信小程序怎么上传图片到服务器吧. 微信小程序怎么上传图片到服务器? 首先, ...

  3. 微信小程序上传图片到服务器总是失败_微信小程序上传图片过大导致请求失败的解决方法...

    很多时候我们都会碰到小程序上传图片时因为图片过大而导致请求失败,同时出现各种各样的问题,那么今天来给大家写一个微信小程序上传图片过大导致请求失败的完美解决办法. 以云开发小程序图片检测为例,如果图片过 ...

  4. 微信小程序上传图片到服务器不显示,微信小程序上传图片到服务器wx.uploadFile...

    项目中肯路能需还定有开都视这讲房哦搞有名需移洁页定会遇到上传文件到服务器端,小程序提供了很有用的ap朋支不器几事为的时后级功发发来久都这样含制层是请些间例业多在上i wxml代码遇新是直朋能到: 上传 ...

  5. uniapp 微信小程序 上传图片到服务器

    图片选择,图片上传,点击查看,点击删除. uniapp自带的chooseImage和uploadFile就可以解决选择和上传.预览/查看用previewImage,删除就直接移除list里面的项就好了 ...

  6. 图片上传成功但是图片显示不出来_小程序上传图片到腾讯云

    这是小程序开发第二篇,主要介绍如何上传图片到腾讯云,之所以选择腾讯云,是因为腾讯云免费空间大 准备工作 上传图片主要是将图片上传到腾讯云对象存储(COS). 要使用对象存储 API,需要先执行以下步骤 ...

  7. 小程序上传图片到七牛云

                                  小程序上传图片到七牛云 一.创建七牛云账号,获取ak.sk,创建对象存储空间名称 二.服务端接口获取七牛token值,个人使用的php编写的 ...

  8. 微信小程序上传图片组件,多选+单选+预览+删除

    微信小程序上传图片+预览+删除 组件代码 HTML <view class="uploadImg-wrap"><view class="upload-f ...

  9. 微信小程序上传图片到七牛云

    七牛云上传图片 第一次用七牛云,目的是存储一些图片 1.首先是七牛云的注册,很简单. 2.注册完创建新的空间 3.这时候会自动分配给你一个域名,但是有效期是30天. 4.若要长久使用,最好用自己的域名 ...

最新文章

  1. effective stl 条款15 小心string实现的多样性
  2. 简单工厂,工厂方法,抽象工厂
  3. 一丶宝塔+青龙面板安装部署教程及命令-依赖库
  4. python ssh模块_windows下python SSH的使用——paramiko模块
  5. android 监听时钟变化,Android4.4 SystemUI分析之Clock时钟显示
  6. java 不同包子类 覆盖_Java中不同方法的覆盖方法
  7. android自动让输入框上划,Android界面技巧:当输入法调出时,如何让界面自动上移,使输入法不会遮挡到主界面(Activity)...
  8. java实验二答案天津商业大学_天津商业大学信息安全实验一
  9. caffe新手常遇到的三个问题
  10. 【嵌入式】非操作系统下GPIO口控制器及LED灯编程
  11. javascript实现blob流、base64,file、base64的互相转换
  12. ui-router 路由重定向
  13. 约瑟夫环c语言代码顺序存储,约瑟夫环问题算法的C语言代码实现
  14. 为什么有些蓝牙耳机有底噪?高音质便宜实惠的蓝牙耳机分享
  15. 解读《肖申克的救赎》
  16. M1卡破解(自从学校升级系统之后,还准备在研究下)
  17. 【python报错解决】findfont: Font family [‘Arial‘] not found. Falling back to DejaVu Sans.
  18. 华三防火墙透明模式典型组网配置实例
  19. 图情论文笔记 | 智慧图书馆下的阅读推广服务策略
  20. 新晋院士,任大学校长!

热门文章

  1. C/C++编程学习 - 第13周 ⑤ 财务管理
  2. QtVirtualKeyboard 虚拟键盘美化
  3. 【后缀自动机】Luogu P3975 [TJOI2015]弦论题解
  4. python生成报表并打印_使用expect+python拉取数据并生成报表
  5. Android Studio中Git更新本地的远程(remote)branch列表?
  6. MTK平台产线生产标记位功能
  7. 6 zzuliPTA家庭土地管理
  8. 《匆匆那年》的你,还记得吗?数学中的那些有(hui)趣(se)的定理(5)——鸡爪定理
  9. 面向对象:余愿,知你冷暖,懂你悲欢,与你共黄昏,也能问你粥可温
  10. 电气成套设备远程监控应用