微信小程序上传图片到html,微信小程序-上传图片

本文详细介绍了在小程序中实现图片上传功能的过程,包括点击上传按钮弹出菜单选择图片或拍照,预览图片,以及将图片上传到服务器的步骤。同时提到了服务器端使用Node.js接收并保存图片的实现,并强调了需通过NGINX部署静态图片库以访问图片资源。
摘要由CSDN通过智能技术生成

在做小冰箱这个项目时,对物品的图片需要有一个上传功能。今天在这里讲一下这个功能是如何实现的。

首先讲述一下这个功能的完整描述:有一个上传图片的按钮,点击按钮,弹出操作菜单:从手机相册选择,拍照。点击从手机相册选择,调起图片库,选择图片;点击拍照,调起摄像头,点击拍照按钮,拍出照片。获取图片后,在上传图片按钮下显示预览图片。点击表单确认按钮,提交。

下面来看一下具体实现:

上传图片按钮

html :

图片:

script :

data = {

statApi : config.statApi,

baseApi : config.baseApi, //http://127.0.0.1

goods_photo: '',

goods_photo_flag: false,

goods_photo_url: '',

flag_tp: false

}

methods = {

uploadImg () {

console.log('shangchan')

let _this = this

wx.showActionSheet({

itemList: ['从手机相册选择', '拍照'],

success (res) {

console.log(res.tapIndex)

if (res.tapIndex == 0) {

_this.chooseImage()

}

if (res.tapIndex == 1) {

_this.toTakePhoto()

}

},

fail (res) {

console.log(res.errMsg)

}

})

}

}

从手机相册选择

chooseImage() {

let _this = this

wx.chooseImage({

count: 1,

sizeType: ['original', 'compressed'],

sourceType: ['album', 'camera'],

success (res) {

// tempFilePath可以作为img标签的src属性显示图片

const tempFilePaths = res.tempFilePaths

console.log('从本地选择相片成功')

_this.goods_photo = tempFilePaths[0]

_this.goods_photo_flag = true

_this.$apply()

_this.uploadImg()

}

})

}

拍照

html:

style:

.sec_camera {

width: 100%;

height: 500rpx;

position: fixed;

top: 0;

z-index: 10;

}

.btn_photo {

width: 100rpx;

height: 100rpx;

border-radius: 100rpx;

position: fixed;

z-index: 10;

left: 50%;

top: 510rpx;

transform: translateX(-50%);

background-color: lavender;

}

script :

toTakePhoto() {

this.flag_tp = true

this.$apply()

}

takePhoto() {

console.log('takePhoto')

let _this = this

const ctx = wx.createCameraContext()

ctx.takePhoto({

quality: 'high',

success: (res) => {

_this.goods_photo = res.tempImagePath

console.log('拍照成功')

_this.goods_photo_flag = true

_this.$apply()

_this.uploadImg()

}

})

}

上传图片到静态资源库

uploadImg() {

let _this = this

wx.uploadFile({

// url: 'http://127.0.0.1/images/upload', //仅为示例,非真实的接口地址

url: _this.baseApi+'/images/upload', //仅为示例,非真实的接口地址

filePath: _this.goods_photo,

name: 'file',

formData: {

'user': 'test'

},

success (res){

const data = JSON.parse(res.data)

console.log('从本地上传相片到服务器成功')

_this.goods_photo_url = data.image_url

_this.flag_tp = false

_this.$apply()

},

fail (res){

console.log('从本地上传相片到服务器失败')

_this.flag_tp = false

_this.$apply()

console.log(res)

}

})

}

服务器上传node实现

var express = require('express');

var app = express();

var formidable = require('formidable');

var fs = require('fs');

app.use('/statics', express.static('statics'));

app.post('/upload', function (req, res) {

var form = new formidable.IncomingForm();

form.parse(req, function(error, fields, files) { fs.writeFileSync("/Users/xz/files/statics/images_user/goods_photos/"+files.file.name,fs.readFileSync(files.file.path));

let image_url = "/statics/images_user/goods_photos/" + files.file.name

res.json({'success':true,'msg':'上传成功!','image_url':image_url});

});

});

module.exports = app;

表单提交

formSubmit(e) {

let form_data = {

url : this.goods_photo_url

}

let _this = this

$.post({

url: '/fridgeGoods/addFridgeGoods',

method: 'POST',

data: form_data,

success: function (res) {

if(res.data.err_code == 0){

//提交成功

}

}

})

}

注意: 要用NGINX部署静态图片库,才可以访问到图片资源

这就是小程序里用到的一个需要照片上传的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值