6***@qq.com
6***@qq.com
  • 发布:2019-05-09 10:52
  • 更新:2021-07-28 12:32
  • 阅读:6316

请问uniapp如何将图片绝对路径转化为BASE64格式?

分类:uni-app

请问如何将地图绝对路径转化为BASE64格式?
我用canvas裁剪一张图片,得到的是图片的路径,需要上传到服务器

_doc/uniapp_temp_1557369968195/canvas/1557369991367.jpg

请问要怎么转化为BASE64格式?

2019-05-09 10:52 负责人:无 分享
已邀请:
hhyang

hhyang - 如有问题,请添加QQ1606726660 备注付费咨询

https://github.com/SilurianYang/uni-app-tools/blob/master/uni-app-h5%2B/expand.js

  • t***@163.com

    这个写的倒是没错,但是实测读取文件无反应。最后研究半下午时间 发现不能传第二个参数,即UTF-8 传了就没反应了。

    2020-01-09 14:41

1***@qq.com

1***@qq.com

解决了吗? 我好像报错呀!

t***@163.com

t***@163.com

async appReadFileToBase64(path){  
        return new Promise((resolve,reject)=>{  

            plus.io.resolveLocalFileSystemURL(path, function(entry) {  
                entry.file(function(file) {  
                    var AppReader = new plus.io.FileReader();  
                    AppReader.onloadend = function(e) {  
                        let base64 = e.target.result  
                        resolve(base64.split(",")[1])  
                    };  
                    AppReader.onerror = function(err) {  
                        reject(err)  
                    };  
                    AppReader.readAsDataURL(file);    
                }, function(e) {  
                    reject(e)  

                });  
            });  

        })  

    }
  • 1***@qq.com

    有例子吗? 这个代码我也测试过好像没有效果

    2020-01-09 16:44

1***@qq.com

1***@qq.com

您好,问题解决了么

ASG

ASG

请问您是怎么解决的啊?

1***@qq.com

1***@qq.com

function getLocalFilePath(path) {
if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {
return path
}
if (path.indexOf('file://') === 0) {
return path
}
if (path.indexOf('/storage/emulated/0/') === 0) {
return path
}
if (path.indexOf('/') === 0) {
var localFilePath = plus.io.convertAbsoluteFileSystem(path)
if (localFilePath !== path) {
return localFilePath
} else {
path = path.substr(1)
}
}
return '_www/' + path
}

export function pathToBase64(path) {
return new Promise(function(resolve, reject) {
if (typeof window === 'object' && 'document' in window) {
if (typeof FileReader === 'function') {
var xhr = new XMLHttpRequest()
xhr.open('GET', path, true)
xhr.responseType = 'blob'
xhr.onload = function() {
if (this.status === 200) {
let fileReader = new FileReader()
fileReader.onload = function(e) {
resolve(e.target.result)
}
fileReader.onerror = reject
fileReader.readAsDataURL(this.response)
}
}
xhr.onerror = reject
xhr.send()
return
}
var canvas = document.createElement('canvas')
var c2x = canvas.getContext('2d')
var img = new Image
img.onload = function() {
canvas.width = img.width
canvas.height = img.height
c2x.drawImage(img, 0, 0)
resolve(canvas.toDataURL())
canvas.height = canvas.width = 0
}
img.onerror = reject
img.src = path
return
}
if (typeof plus === 'object') {
plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
entry.file(function(file) {
var fileReader = new plus.io.FileReader()
fileReader.onload = function(data) {
resolve(data.target.result)
}
fileReader.onerror = function(error) {
reject(error)
}
fileReader.readAsDataURL(file)
}, function(error) {
reject(error)
})
}, function(error) {
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
wx.getFileSystemManager().readFile({
filePath: path,
encoding: 'base64',
success: function(res) {
resolve('data:image/png;base64,' + res.data)
},
fail: function(error) {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}

export function base64ToPath(base64) {
return new Promise(function(resolve, reject) {
if (typeof window === 'object' && 'document' in window) {
base64 = base64.split(',')
var type = base64[0].match(/:(.*?);/)[1]
var str = atob(base64[1])
var n = str.length
var array = new Uint8Array(n)
while (n--) {
array[n] = str.charCodeAt(n)
}
return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
}
var extName = base64.match(/data\:\S+\/(\S+);/)
if (extName) {
extName = extName[1]
} else {
reject(new Error('base64 error'))
}
var fileName = Date.now() + '.' + extName
if (typeof plus === 'object') {
var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
bitmap.loadBase64Data(base64, function() {
var filePath = '_doc/uniapp_temp/' + fileName
bitmap.save(filePath, {}, function() {
bitmap.clear()
resolve(filePath)
}, function(error) {
bitmap.clear()
reject(error)
})
}, function(error) {
bitmap.clear()
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
var filePath = wx.env.USER_DATA_PATH + '/' + fileName
wx.getFileSystemManager().writeFile({
filePath: filePath,
data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
encoding: 'base64',
success: function() {
resolve(filePath)
},
fail: function(error) {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}

该问题目前已经被锁定, 无法添加新回复