首先我用vue上传阿里图片用的是分片上传,分片上传由于一片是以100kb为起始的,所以当图片大小小于100kb的时候不分片,可以正常上传,当大于100kb的时候,会报400错误如下

  1. One or more of the specified parts could not be found or the specified entit

当报这个错误时你要去登录阿里后台设置一下

  1. 文档地址:https://help.aliyun.com/document_detail/32069.htm
  2. exopose header 要设置为 ETag

设置如下图

当你设置好了之后发现大于100kb也可以上传了,只不过阿里返回的图片链接格式有所不同

大于100kb的图片链接?后面带了一些参数你要截取

下面是我的代码,运用了element组件库,包含了图片上传阿里,图片压缩

  1. <template>
  2. <div class="fabu">
  3. <div class="d1">
  4. <div class="dd1">
  5. <textarea placeholder="想要说的..." v-model="form.comment"></textarea>
  6. <el-upload
  7. action=""
  8. :http-request="zidingyi_upload"
  9. list-type="picture-card"
  10. ref="upload"
  11. :limit="3"
  12. :auto-upload="false"
  13. :on-preview="handlePictureCardPreview"
  14. :on-remove="handleRemove"
  15. :on-exceed="handleExceed"
  16. :before-remove="beforeRemove"
  17. :file-list="fileList"><!--auto-upload是否在选取文件后立即进行上传 exceed超出限制--><!--http-request点击发布--><!--:on-progress上传进度-->
  18. <i class="el-icon-plus"></i>
  19. </el-upload>
  20. </div>
  21. <!--图片查看-->
  22. <el-dialog :visible.sync="dialogVisible">
  23. <img width="100%" :src="dialogImageUrl" alt="">
  24. </el-dialog>
  25.  
  26. <div class="dd2"><p>红包个数</p><p><input type="number" v-model="form.packet_count" placeholder="填写个数"/></p></div>
  27. <div class="dd3"><p>单个金额</p><p><input type="number" v-model="form.single_Amount" placeholder="填写金额"/></p></div>
  28.  
  29. <el-button class="fa" @click="submitUpload">发布</el-button>
  30. </div>
  31.  
  32. </div>
  33. </template>
  34.  
  35. <script>
  36. // var OSS = require('ali-oss')
  37. export default{
  38. name:'fabu',
  39. data(){
  40. return{
  41. dialogImageUrl: '',
  42. dialogVisible: false,
  43. fileList:[],
  44.  
  45. img_url_num:[],//用户发布时的图片
  46.  
  47. user_token:'',
  48. form:{//发布时的参数
  49. comment:'',
  50. imagea:'',
  51. imageb:'',
  52. imagec:'',
  53. lat:'',
  54. lng:'',
  55. single_Amount:'',//红包单价
  56. packet_count:'',//红包个数
  57. },
  58.  
  59. }
  60. },
  61. mounted(){
  62. this.user_token=localStorage.getItem('user_token')
  63. this.form.lat=localStorage.getItem('lat')
  64. this.form.lng=localStorage.getItem('lng')
  65. },
  66. methods:{
  67. //自定义点击上传
  68. submitUpload(){
  69. if(this.$refs.upload.submit()!=undefined){//如果用户点击发布时有上传图片
  70. if(this.form.comment==''){
  71. this.$alert('你还没有填写发布内容','提示',{
  72. confirmButtonText: '确定',
  73. callback:action => {
  74. }
  75. })
  76. return
  77. }
  78. this.$refs.upload.submit()
  79. }else{//如果用户点击发布时没有有上传图片
  80. if(this.form.comment==''){
  81. this.$alert('你还没有填写发布内容','提示',{
  82. confirmButtonText: '确定',
  83. callback:action => {
  84. }
  85. })
  86. return
  87. }
  88. this.userLaunchEvent2()
  89. }
  90.  
  91. },
  92. //移除图片
  93. handleRemove(file, fileList) {
  94. console.log(file, fileList);
  95. },
  96. //点击文件列表中已上传的文件时的钩子,可修改放大图片及删除
  97. handlePictureCardPreview(file){
  98. this.dialogImageUrl = file.url;
  99. this.dialogVisible = true;
  100. },
  101. //超出限制
  102. handleExceed(files, fileList) {
  103. this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
  104. },
  105. //删除图片
  106. beforeRemove(file, fileList) {
  107. return this.$confirm(`确定移除 ${ file.name }?`);
  108. },
  109. //自定义图片上传
  110. zidingyi_upload(event){
  111. console.log(event)
  112. //先压缩在上传
  113. //利用H5的FileReader对象将上传的图片转成base64格式
  114. this.h5_reader(event.file)
  115. },
  116. //阿里图片上传
  117. doUpload(file){
  118. let that =this
  119. const client = new OSS.Wrapper({
  120. secure: false,//http
  121. region: 'oss-cn-hangzhou',
  122. accessKeyId: '9eEB2RAzLHaW5Asu',
  123. accessKeySecret: 'YdbWni4mRxDy0ndN6g4xH9ErpkV5ZT',
  124. bucket: 'xiangcaoshequ'/*装图片的桶名*/
  125. })
  126. //获取图片的的格式
  127. let type = file.type.split('/')[1];
  128. //自定义上传的名字
  129. let name = 'xcb'+file.lastModified+'.'+type;
  130. // let name = 'lz'+this.user_phone+'.'+type;
  131.  
  132. // console.log(type)
  133.  
  134. if(file.size>this.fileSize){
  135. this.$alert('亲,你上传的头像太大了,不能大于1M哦', '提示', {
  136. confirmButtonText: '确定',
  137. callback: action => {
  138. }
  139. });
  140. return
  141. }
  142. client.multipartUpload(name, file,{
  143. progress:function*(percentage, cpt){
  144. // 上传进度
  145. that.percentage = percentage
  146. console.log(percentage)
  147. }
  148. }).then((result)=> {
  149. console.log(result)//至此就拿到了返回的路径
  150. //得到上传后的地址
  151. let user_img = result.res.requestUrls[0];
  152. //因为图片压缩之后大于100kb的情况下返回的图片路径有?而小于100kb没有,所以我做以下处理
  153. let img_url=user_img.split('?')[0]
  154. //上传是的图片路径放到数组里
  155. this.img_url_num.push(img_url)
  156. //调用后台接口
  157. this.userLaunchEvent()
  158. }).catch(function (err) {
  159. console.log(err);
  160. });
  161. },
  162. // 压缩图片
  163. compress(img) {
  164. let canvas = document.createElement("canvas");
  165. let ctx = canvas.getContext("2d");
  166. let initSize = img.src.length;
  167. let width = img.width;
  168. let height = img.height;
  169. canvas.width = width;
  170. canvas.height = height;
  171. // 铺底色
  172. ctx.fillStyle = "#fff";
  173. ctx.fillRect(0, 0, canvas.width, canvas.height);
  174. ctx.drawImage(img, 0, 0, width, height);
  175.  
  176. //0.1进行最小压缩
  177. let ndata = canvas.toDataURL("image/jpeg", 0.5);
  178. // console.log("*******压缩后的图片大小*******");
  179. // console.log(ndata)
  180. // console.log(ndata.length);
  181. return ndata;
  182. },
  183. //利用H5的FileReader对象将上传的图片转成base64格式
  184. h5_reader(file){
  185. var that=this
  186. //创建一个reader
  187. let reader = new FileReader();
  188. //将图片转成base64格式
  189. reader.readAsDataURL(file);
  190. //读取成功后的回调
  191. reader.onloadend = function() {
  192. let result = this.result;
  193. let img = new Image();
  194. img.src = result;
  195. console.log("********未压缩前的图片大小********");
  196. console.log(result.length);
  197. img.onload = function() {
  198. let data = that.compress(img);
  199. that.imgUrl = result;
  200.  
  201. let blob = that.dataURItoBlob(data);
  202.  
  203. console.log("*******base64转blob对象******");
  204. console.log(blob);
  205.  
  206. var formData = new FormData();
  207. formData.append("file", blob);
  208. console.log("********将blob对象转成formData对象********");
  209. console.log(formData.get("file"));
  210.  
  211. //压缩之后再阿里上传
  212. let event =formData.get("file")
  213. that.doUpload(event)
  214. };
  215. };
  216.  
  217. },
  218. // base64转成bolb对象
  219. dataURItoBlob(base64Data) {
  220. var byteString;
  221. if (base64Data.split(",")[0].indexOf("base64") >= 0)
  222. byteString = atob(base64Data.split(",")[1]);
  223. else byteString = unescape(base64Data.split(",")[1]);
  224. var mimeString = base64Data
  225. .split(",")[0]
  226. .split(":")[1]
  227. .split(";")[0];
  228. var ia = new Uint8Array(byteString.length);
  229. for (var i = 0; i < byteString.length; i++) {
  230. ia[i] = byteString.charCodeAt(i);
  231. }
  232. return new Blob([ia], { type: mimeString });
  233. },
  234. //得到上传后的三个图片路径,调用接口
  235. //阿里图片上传之后的请求的发布内容
  236. userLaunchEvent(){
  237. this.form.imagea=this.img_url_num[0]
  238. this.form.imageb=this.img_url_num[1]
  239. this.form.imagec=this.img_url_num[2]
  240. if(this.form.imageb==undefined){
  241. this.form.imageb=''
  242. }
  243. if(this.form.imagec==undefined){
  244. this.form.imagec=''
  245. }
  246. console.log(this.form)
  247. this.$http.post('https://www.52herb.com/yuandi/campaign/userLaunchEvent?token='+this.user_token,this.form).then((res)=>{
  248. console.log(res)
  249. })
  250. },
  251. //如果用户没有上传图片只是发了内容
  252. userLaunchEvent2(){
  253. console.log(this.form)
  254. this.$http.post('https://www.52herb.com/yuandi/campaign/userLaunchEvent?token='+this.user_token,this.form).then((res)=>{
  255. console.log(res)
  256. })
  257. },
  258. //微信支付
  259. zhifu_money(){
  260. let url = window.location.href.split('#')[0];
  261. url = encodeURIComponent(url)
  262. this.$http.get('https://www.52herb.com/yuandi/app/weixinSign?url='+url).then((res)=>{
  263. wx.config({
  264. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  265. appId: 'wx0e8a3c02faa5d089',
  266. timestamp: res.body.data.timestamp,
  267. nonceStr:res.body.data.nonceStr,
  268. signature: res.body.data.signature,
  269. jsApiList: [
  270. 'chooseWXPay'
  271. ] // 必填,需要使用的JS接口列表
  272. });
  273. })
  274.  
  275. let user_money = 0;
  276. let money = input_value*100;//产品价格
  277. let pay_type = 'reachW'//微信支付
  278. this.$http.get("https://www.52herb.com/yuandi/pay/reach/weixin/preorderh5?money="+money+"&user_money="+user_money+"&pay_type="+pay_type+"&openId="+this.openid+"&shop_guid="+this.shop_guid+"&token="+this.user_token+"&notify_url=http://www.xiangcaobang.com/webapp/saoma.html&status=1").then((res)=>{
  279. // alert(JSON.stringify(res))
  280. var orderId =res.body.orderId;
  281. wx.chooseWXPay({
  282. timestamp: res.body.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  283. nonceStr: res.body.nonceStr, // 支付签名随机串,不长于 32 位
  284. package: res.body.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  285. signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  286. paySign: res.body.paySign, // 支付签名
  287. success: function (res) {
  288. // 支付成功后的回调函数
  289. that.$http.post("https://www.52herb.com/yuandi/pay/weixin/updateUserOrder?orderId="+orderId+"&token="+that.user_token+"&method=2").then((res)=>{
  290. // alert(JSON.stringify(res))
  291. if(res.body.code==0){
  292. that.$confirm('支付成功', '提示', {
  293. confirmButtonText: '继续支付',
  294. cancelButtonText: '返回首页',
  295. type: 'warning'
  296. }).then(() => {
  297. //获取用户当天余额抵扣多少钱
  298. that.huoqucishu()
  299. }).catch(() => {
  300. that.$router.push({name:'Index'})
  301. });
  302. }
  303. })
  304.  
  305. }
  306. })
  307. })
  308.  
  309. }
  310.  
  311. }
  312. }
  313. </script>
  314.  
  315. <style scoped="scoped">
  316. /*.fa{font-size: 0.3rem;width: 50%;height: 0.8rem;line-height: 0.8rem;background: green;margin: auto;text-align: center;}*/
  317. .up .el-upload{width: 0.5rem;height: 0.5rem;}
  318.  
  319. .fabu{background: #f0f0f0;position: absolute;width: 100%;height: 100%;}
  320. .d1 .dd1{height: 4rem;background: white;padding: 0.2rem 0.2rem 0 0.2rem;}
  321. .d1 textarea{height: 2rem;width: 100%;padding: 0.1rem;box-sizing: border-box;line-height: 0.35rem;color: orange;}
  322.  
  323. .d1 .dd2,.d1 .dd3{padding: 0 0.2rem;box-sizing: border-box; background: white;height: 0.8rem;line-height: 0.8rem;display: flex;font-size: 0.3rem;margin-top: 0.2rem;justify-content: space-between;}
  324. .d1 .dd2 input,.d1 .dd3 input{width: 1.5rem;}
  325. </style>

vue阿里上传图片报400错误的更多相关文章

  1. MVC中使用jquery uploadify上传图片报302错误

    使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...

  2. Yii2请求,报400错误

    出现400错误是yii2.0的csrf防范策略导致 在components里面添加request配置如下: 'request' => [ // !!! insert a secret key i ...

  3. 访问本机的WEB API 报400错误

    当时用的IP是127.0.0.1 报400错误,换成 localhost 后正常.

  4. SpringMVC提交数据遭遇基础类型和日期类型报400错误解决方法

    使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 .下面是解决方案的演示示例: 这个是实体类,里面createDate就是java ...

  5. Yii 1.1 请求报400错误

    Yii的action可以带参数,比如: class PostController extends CController { public function actionCreate($categor ...

  6. swagger-ui中测试接口(enum传值) 报400错误

    swagger-ui中测试接口(enum传值) 报400错误 PriceRuleController: @PostMapping("/update") @ApiOperation( ...

  7. cdnbest里站点域名不同步到节点,报400错误的一般原因

    报400错误一般是站点里的域名没有同步到节点上面的原因,产生的原因一般是下面两点原因: 1.检查节点列表如下图所示的状态是否打钩,这是节点和主控的通信状态,打叉表示连接有问题 这里打叉的几种原因(1) ...

  8. 解决nginx转发websocket报400错误

    解决nginx转发websocket报400错误 说明 由于个人服务器上面有多个项目,配置了二级域名,需要对二级域名进行转发,在转发工作这快采取了大名鼎鼎的nginx.在这之前所有的项目运行转发都没问 ...

  9. js 记一次带时间的表单提交报400错误

    写一个功能的时候,表单里不填时间提交的时候,数据就正常传到后台了,一填上时间就报400错误,看了后台时间的处理也没问题,看了前端时间控件返回的格式也对,但是就是一直报错, 把提交的数据打印出来也没发现 ...

随机推荐

  1. Docker 容器启动 查看容器状态 - 四

    1.容器两种方式进行启动 一种是基于创建一个容器并启动 docker create docker start 另一种 使用 run 创建自动启动:是状态下的停止 启动 docker start ngi ...

  2. 电脑丢失api-ms-win-core-libraryloader-|1-1-1.dll怎么办

    电脑从win7升级到win10,到98%的时候提示说丢失.dll,如图,我是64位系统,怎么解决这个问题呢?在脚本之家下载了 放到system32中也没有用,在线等,谢谢! 用C:\Windows\S ...

  3. 第20月第4天 pycharm utf-8

    1.运行python %run a.py 运行 https://blog.csdn.net/little_bobo/article/details/78982412 2.UnicodeDecodeEr ...

  4. Windows jdk安装以及版本切换

    Windows jdk版本切换 一.安装 1.下载 官网: Java SE Development Kit 8 Downloads Java SE 7 Archive Downloads 1.7之前的 ...

  5. Python 爬虫六 性能相关

    前面已经讲过了爬虫的两大基础模块: requests模块:用来伪造请求爬取数据 bs4模块:用来整理,提取数据 当我们真正的开始有需求的时候通常都是批量爬取url这样的.那如何批量爬取呢? 按照正常的 ...

  6. D - The Lucky Week ZOJ - 3939 (思维)

    题目链接: D - The Lucky Week  ZOJ - 3939 题目大意:幸运的星期指,星期一为每个月的1 or 11 or 21号.给出第一个幸运星期的时间,问从当前的日起开始.第n个的日 ...

  7. DeepLearning.ai-Week2-Residual Networks

    1 - Import Packages import numpy as np from keras import layers from keras.layers import Input, Add, ...

  8. SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询

    前言:本文档使用的是 SpringBoot,如果是 Spring 还需要在 MyBatis 配置 xml 中配置拦截器,并且 PageHelper 是针对 MyBatis 的,MyBatis 的集成不 ...

  9. 2017-2018-2 20165234 实验二 《Java面向对象程序设计》实验报告

    一.实验报告封面 课程:Java程序设计  班级:1652班  姓名:刘津甫  学号:20165234 指导教师:娄嘉鹏  实验日期:2018年4月13日 实验时间:15:35 - 17:15  实验 ...

  10. Python对HDFS的一些基础操作

    链接: http://www.cnblogs.com/shoufengwei/p/5949791.html