English 中文(简体)
• 如何在信道上或作为HtmlImageElement(<img>)显示浏览器中的落成物。
原标题:how to display a dropped image file in browser on a canvas or as a HtmlImageElement (<img>)

i actually wondering why the only way of doing this is taking the binary data from the file, base64-encode it and then feed this encoded String into an Tag / HtmlImageElement

要么简单 传真

<img src="data:image/jpeg;base64,xxxxxxxxxxxxx...">

let reader = new FileReader();
reader.readAsDataURL( droppedFile ); // this converts the binary data from the file into base64
reader.onloadend = () => {
    const img = new Image();
    img.src = reader.result;
    img.onloadend = () => { 
        console.log(`image ${droppedFile.name} loaded.`);
    }  
}

出色的业绩: 数据是基64-编码,将1MB图像转换成1.33MBSting(这确实需要时间),然后放在图像Element上,然后才重新编码(再次需要时间)。

为什么如此?

确实没有办法使用“PetdArray”(UInt8Arrays)?

浏览器在哪里对档案类型进行核对,并检索 let子和肥料的高度,请说丢弃了jpg文档?

问题回答

我知道另一种最可能更快的方式。 你直接拿到档案,将其改为反对L。

这样:

const input = document.querySelector("input"),
    img = document.querySelector("img")

input.addEventListener("change", () => {
    if (input.files.length) {
        const file = input.files[0],
            url = (window.URL || window.webkitURL).createObjectURL(file)

        img.src = url
    } else {
        img.src = ""
    }
})
<input type="file" />
<img src="" />




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签