当前位置:网站首页>JS image conversion Base64 Base64 conversion to file object
JS image conversion Base64 Base64 conversion to file object
2022-07-24 17:19:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
js Picture conversion base64 base64 Convert to file object
function getImgToBase64(url,callback){
// Convert the picture to Base64
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL('image/png');
callback(dataURL);
canvas = null;
};
img.src = url;
} function dataURLtoFile(dataurl, filename) {
// take base64 Convert to file
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}// You can convert pictures to base64
getImgToBase64('img/test.png',function(data){
var myFile = dataURLtoFile(data,'testimgtestimgtestimg');
console.log(myFile);
});relevant : https://www.cnblogs.com/fps2tao/p/9268380.html
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/107436.html Link to the original text :https://javaforall.cn
边栏推荐
- 1309. Decode letter to integer mapping
- One article of quantitative framework backtrader: understand indicator indicators
- It's time to consider slimming down your app
- Summary of ROS master-slave communication experience
- 详解 Apache Hudi Schema Evolution(模式演进)
- 内核开发
- Is computer monitoring true? Four experiments to find out
- Development dynamics | stonedb 2022 release milestone
- Check the actual data growth of the database
- Still developing games with unity? Then you're out. Try unity to build an answer system
猜你喜欢

Internet Download Manager配置
![[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation](/img/5f/c790baf8f8e62fca36fdb4492c38b2.png)
[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation

Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection

CDN (content delivery network) content distribution network from entry to practice

Axi protocol (3): handshake mechanism and implementation details of Axi architecture

Portfwd port forwarding
![[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~](/img/10/996d594a53d9a34a36079fed829f27.png)
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~

Internet Download Manager Configuration

Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation

Heuristic merging (including examples of general formula and tree heuristic merging)
随机推荐
Concept of IP, classification of IP, IP multiplexing technology
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
PAT甲级——A + B 格式
2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
portmap 端口转发
quick get start
Axi protocol (2): five channels and two transactions of Axi architecture
Transformer structure analysis -- learning notes
QML coding specification
ufw 端口转发
Scroll bar adjust brightness and contrast
NATBypass 端口转发
Internet Download Manager Configuration
安全:如何为行人提供更多保护
滚动条调整亮度和对比度
Portfwd port forwarding
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
[matlab]: basic knowledge learning
opencv自带颜色操作
快速入门