uni.canvasToTempFilePath 截图没有后缀名 blob路径

后端拼接完成
博客提及后端拼接完成,但信息较少。推测可能是后端开发中某部分工作达成拼接成果,与后端开发相关。

然后端拼上了

### 使用 `uni.canvasToTempFilePath` 并重命名生成的临时文件 当调用 `uni.canvasToTempFilePath` 方法时,会返回一个表示图片保存成功后的本地临时文件路径的对象。为了实现对该临时文件路径所指向文件的重命名操作,可以采用如下方法: #### 实现思路 由于微信小程序环境中的文件系统接口限制,无法直接对临时文件进行重命名操作。因此,解决方案是先将该临时文件保存到应用的本地缓存目录下并赋予新的名称,再删除原始的临时文件。 #### 代码示例 下面是一个具体的 JavaScript 函数实现,用于处理 canvas 转换为图像后对其进行重命名的操作[^1]: ```javascript function renameCanvasFile(canvasId, newName) { const tempFilePath = uni.canvasToTempFilePathSync({ canvasId: canvasId, success(res) { console.log('canvas转tempfile成功', res.tempFilePath); // 将临时文件移动至本地缓存,并指定新名字 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success(saveRes) { console.log('save image to album success'); // 获取当前时间戳作为唯一标识符的一部分 let timestamp = Date.now(); let newFileName = `${newName}_${timestamp}.png`; // 移动文件到本地缓存目录 plus.io.resolveLocalFileSystemURL('_doc/', function(entry){ entry.getFile(newFileName, {create:true}, function(fileEntry){ fileEntry.createWriter(function(writer){ writer.onwriteend = function(e){ console.log("Successful file write..."); // 删除原来的临时文件 plus.io.resolveLocalFileSystemURL(res.tempFilePath, function(tempFileEntry){ tempFileEntry.remove(function(){ console.log("Original tempfile removed."); }, function(error){ console.error("Failed to remove original tempfile.", error); }); }); }; var xhr = new XMLHttpRequest(); xhr.open("GET", saveRes.savedFilePath, true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status === 200) { writer.write(this.response); } }; xhr.send(); }, function(error){ console.error("Create file failed.", error); }); }, function(error){ console.error("Get file entry failed.", error); }); }); }, fail(err) { console.error('Save image failed.', err); } }); }, fail(err) { console.error('Convert canvas to tempfile failed.', err); } }); } ``` 此函数接收两个参数:一个是 Canvas 组件 ID (`canvasId`);另一个是要给转换得到的新文件设置的名字(`newName`)。注意这里使用了 HBuilderX 提供的一些 API 来完成实际的任务,在其他环境中可能需要调整相应的部分来适配同的平台特性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值