uniapp图片临时路径-转网络地址|转base64上传

做了小程序的拍照功能,需要将照片上传

记录将临时路径图片上传给后端的两种办法

1.网络地址上传

// 拍照方法
    handleTakePhotoClick() {
      const ctx = uni.createCameraContext();
      ctx.takePhoto({
        quality: "high",
        success: (res) => {
          // 获取网络地址
           uni.uploadFile({
             url:"",//upload接口地址
             filePath: res.tempImagePath,//res.tempImagePath为临时路径
             name: "file",
             // header: {
             //  accessToken: storage.getAccessToken(),
             //},
             success: (uploadFileRes) => {
               //获得网络地址进行后续操作
             },
           });
        },
      });
    },

2.临时地址转base64上传

   // 拍照方法
    handleTakePhotoClick() {
      const ctx = uni.createCameraContext();
      ctx.takePhoto({
        quality: "high",
        success: (res) => {
          // 获取base64
           uni.getFileSystemManager().readFile({
                filePath: res.tempImagePath, //照片的临时地址
                encoding: "base64", //编码格式
                success: (res) => {
                    let data="data:image/jpg" +";base64," +res.data //base64地址
                    //后续操作
                },
                fail: (res) => reject(res.errMsg),
           })
        },
      });
    },

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Base64 编码的字符串换为 Word 文件,可以按照以下步骤进行: 1. 将 Base64 编码的字符串解码为二进制数据。 2. 将二进制数据写入一个临时文件中。 3. 使用 Word 应用程序对象打开该临时文件。 4. 将该文件另存为 Word 格式。 下面是一个 Python 代码示例,实现了将 Base64 编码的字符串换为 Word 文件的功能: ```python import base64 import tempfile import win32com.client as win32 def base64_to_word(base64_str, output_path): # 解码Base64字符串为二进制数据 data = base64.b64decode(base64_str) # 将二进制数据写入临时文件 with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(data) tmp.flush() # 使用Word应用程序对象打开临时文件 word = win32.gencache.EnsureDispatch('Word.Application') doc = word.Documents.Open(tmp.name) # 另存为Word格式 doc.SaveAs(output_path, FileFormat=16) # 关闭Word文档和应用程序对象 doc.Close() word.Quit() ``` 这个函数接受两个参数:Base64 编码的字符串和输出路径。它将 Base64 编码的字符串解码为二进制数据,并将其写入一个临时文件中。然后,它使用 Word 应用程序对象打开该临时文件,并将其另存为 Word 格式。最后,它关闭 Word 文档和应用程序对象。 使用示例: ```python base64_str = "SGVsbG8gV29ybGQh" output_path = "hello.docx" base64_to_word(base64_str, output_path) ``` 这个示例将 Base64 编码的字符串解码为二进制数据,并将其写入名为 `tmpXXXXX` 的临时文件中。然后,它使用 Word 应用程序对象打开该临时文件,并将其另存为 Word 格式到 `hello.docx` 文件中。最后,它关闭 Word 文档和应用程序对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值