uniapp 上传图片临时文件转成Blob
时间: 2023-11-29 19:46:27 浏览: 98
以下是uniapp上传图片临时文件转成Blob的方法:
```javascript
// 引入toBlob插件
import toBlob from 'toblob'
// 将图片转为base64
let base64 = 'data:image/png;base64,iVBORw0KG...'
// 将base64转为Blob
toBlob.base64ToBlob({b64data: base64, contentType: 'image/png'}).then(res => {
// res即为转换后的Blob对象
console.log(res)
})
```
相关问题
uniapp手机上传图片blob转成base64
你可以使用uniapp的`uni.getImageInfo`方法获取图片的信息,然后使用`uni.getFileSystemManager().readFile`方法将图片转换为base64格式。下面是一个示例代码:
```javascript
uni.getImageInfo({
src: imagePath,
success: function (res) {
uni.getFileSystemManager().readFile({
filePath: res.path,
encoding: 'base64',
success: function (data) {
var base64Image = 'data:image/' + res.type + ';base64,' + data.data;
console.log(base64Image);
},
fail: function (error) {
console.log(error);
}
});
},
fail: function (error) {
console.log(error);
}
});
```
在上面的代码中,`imagePath`是图片的路径。首先使用`uni.getImageInfo`方法获取图片的信息,然后使用`uni.getFileSystemManager().readFile`方法读取图片文件并将其转换为base64格式。最后,可以在成功回调函数中获取到转换后的base64图片数据。
请注意,这个方法需要在真机上测试,因为在H5端可能会受到跨域限制。
php怎么把图片文件转成blob,存入数据库
要将图片文件转换成 Blob 并存储到数据库中,可以按照以下步骤进行:
1. 读取图片文件内容,可以使用 file_get_contents 函数。
2. 将读取到的内容使用 base64_encode 函数进行编码。
3. 将编码后的字符串存储到数据库的 Blob 类型字段中。
下面是一个示例代码,演示了如何将图片文件转换成 Blob 并存储到 MySQL 数据库中:
```php
// 读取图片文件
$imageData = file_get_contents('path/to/image.jpg');
// 将图片数据编码为 base64 字符串
$imageBase64 = base64_encode($imageData);
// 将 base64 字符串存储到数据库中
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$statement = $pdo->prepare('INSERT INTO images (image) VALUES (:image)');
$statement->bindValue(':image', $imageBase64, PDO::PARAM_LOB);
$statement->execute();
```
在上述代码中,我们首先使用 file_get_contents 函数读取了图片文件的内容,然后使用 base64_encode 函数将其编码为 base64 字符串。最后,我们使用 PDO 对象将 base64 字符串存储到数据库中。这里使用了 PDO::PARAM_LOB 参数来指定参数类型为 LOB 类型,以便正确地存储 Blob 数据。