微信小程序 editor图片上传到node服务器并展示在当前页面

文章描述了一个使用uni-app开发的前端应用中,如何通过HTML标签``实现富文本编辑,并集成图片上传功能。当用户点击添加图片按钮时,uni-app调用`chooseImage`接口选择图片,然后上传到由Node.js搭建的后端服务器。后端接收到图片后,保存到指定目录并返回图片路径给前端,前端再将图片插入到编辑器中。
摘要由CSDN通过智能技术生成

前端 

html

<!-- 富文本 -->
			  <view  class="container">
			    <editor id="editor" ref="editor"  :placeholder=placeholder  @input="onInput"  @ready="onEditorReady">
			    </editor>
			  </view>
			<view class="addForum_image" >
				<image src="https://img.icons8.com/officel/80/add-image.png" alt="picture" mode='aspectFit' @tap="insertImage"/>	
				<!-- <image src="https://img.icons8.com/ultraviolet/80/add-image.png" alt="picture" mode='aspectFit'/> -->
			</view>

js

// 上传图片到服务器
			insertImage() {
			        var that = this
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
						sourceType: ['album'], //从相册选择
						success: function (res) {
							console.log(JSON.stringify(res.tempFilePaths));
							const tempFilePaths = res.tempFilePaths
							uni.uploadFile({
							      url: httpUrl+'/aydoorumomg',
							      filePath: tempFilePaths[0],
							      name: 'file',
							      success(uploadFileRes) {
							        const data = JSON.parse(uploadFileRes.data).location
									console.log(data);
							        // 上传成功,获取服务器返回的图片路径和名称
							        // 将图片信息存储至数据库或文件
									that.editorCtx.insertImage({
									      src: data,
									    })
							      }
							    })
						}
					});
			},
			

node后端

//上传论坛帖子图片
    adoporomIog(req,res){
        console.log('上传文件');
        console.log(req.file);
        const file = req.file
        const fileName = `${Date.now()}-${file.filename}.${file.originalname.slice(file.originalname.lastIndexOf('.') + 1)}`
        const uploadFile = fs.createWriteStream(`./public/images/forumimages/${fileName}`,{
            highWaterMark:1024*1024*50
        })
        req.socket.setTimeout(120000)
        req.setTimeout(120000)
        const fileStream = fs.createReadStream(file.path)
        let uploadComplete = false  
        fileStream.on('error', err => {
            uploadFile.close()
        })  
        fileStream.pipe(uploadFile).on('finish', () => {
        uploadComplete = true
        uploadFile.close()  
    })
    res.json({ 
        location: `https://gothordoeaos.com:9990/images/forumimages/${fileName}` 
    })
    },

效果  小程序端

 

 

后台

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值