小程序附件上传并且名称回显

本文介绍了如何使用VantWeapp框架开发小程序,实现文件上传功能,通过调用Java后端的MongoDB接口,并展示了分页查询和附件回显的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1-先看样式 ,一块样式用到 Vant Weapp  感觉挺不错的一个框架就是功能少 了一点 

附上地址 框架地址:Vant Weapp - 轻量、可靠的小程序 UI 组件库

 

2-wxml

<view>
  <input type="text" value="附件名称:{{username}}" bindtap="choImg" />
  <van-button type="primary" bindtap="loadimg">确定上传</van-button>
</view>

 2- js 这个 input 点击上传的附件的信息 而且得到附件名称的回显

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'点击上传附件',
    usernameUrl:'',
},
  
choImg: function () {
    var thiss=this
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res){
           const tempFilePaths =res.tempFilePaths
           thiss.setData({
            username: res.tempFiles[0].name,
            usernameUrl:res.tempFiles[0].path
          })      
           console.log('选择',res) 
      }
    })
 },

 3- js 确定按钮上传的方式  这一块是用到java 后端mongoDB服务器 接收的

 loadimg: function () {
  var _this = this;
  console.log( _this.tempFiles);
  wx.uploadFile({
    url: 'http://localhost:8080/file/uploadFile', //接口
    filePath: _this.data.usernameUrl,
    name: 'file',
    success: function (res) {
      var data = res.data;
      wx.showToast({
        title: '操作成功!', // 标题
        icon: 'success',  // 图标类型,默认success
        duration: 1500  // 提示窗停留时间,默认1500ms
      })
    },
    fail: function (error) {
      wx.showToast({
        title: '操作失败!', // 标题
        icon: 'success',  // 图标类型,默认success
        duration: 1500  // 提示窗停留时间,默认1500ms
      })
      console.log(error);
    }
  })
},

4-看下上传的东西的成果 这一快就是用到分页查询了 上传附件并且名称回显就完成了

 

### 微信小程序富文本编辑器实现功能 在微信小程序中,`<editor>` 组件用于处理富文本输入。当尝试将 HTML 字符串到 `<editor>` 中时,可能会遇到各种问题,特别是涉及到图片尺寸过大等问题[^1]。 为了确保富文本能够成功至编辑区,可以采用如下方法: #### 处理HTML字符串中的图片路径和大小 对于包含较大图像的情况,在设置内容之前应先调整这些资源的属性。可以通过正则表达式或其他方式遍历并修改始 HTML 文本内的 img 标签,指定合适的宽度高度或者最大宽高限制来防止显示异常。 ```javascript // 对html进行预处理,调整img标签样式 function preprocessHtml(htmlString) { return htmlString.replace(/<img([^>]+)>/gi, '<img$1 style="max-width:100%;height:auto;" />'); } ``` #### 使用_onEditorReady调完成初始化工作 通过监听 `_onEditorReady` 事件获取 editor 上下文对象 `that.editorCtx` ,进而调用其提供的 API 方法如 `setContents()` 来加载外部传入的内容片段。 ```typescript _onEditorReady: function (html: string) { const that = this; wx.createSelectorQuery() .select('#editor') .context((res) => { if (!res || !res.context) return; that.editorCtx = res.context; // 调整后的html再传递给 setContents 函数 let processedHtml = preprocessHtml(html); that.editorCtx.setContents({ html: processedHtml, success(res) {}, fail(err) {} }); }) .exec(); }, ``` 上述代码展示了如何安全有效地把经过适当转换过的 HTML 片段注入到微信小程序的 `<editor>` 控件里去,并解决了由于图片尺寸不合适造成的渲染失败问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值