简体   繁体   English

是否可以将图像本地加载到网页中?

[英]Is it possible to load an image into a web page locally?

The idea is to take an image from a user's machine and allow them to display the image in a webpage. 这个想法是从用户的机器上获取图像,并允许他们在网页中显示图像。 I do not want to send the image back to the server. 我不想将图像发送回服务器。

An upload button will exist. 将存在一个上传按钮。 It should just update the page content dynamically. 它应该只是动态地更新页面内容。

Can this be done with HTML5 localstorage or anything? 可以使用HTML5 localstorage或其他方式完成此操作吗?

FileReader is capable of doing this. FileReader能够做到这一点。 Here's sample code: 这是示例代码:

<input type="file" id="files" name="files[]" multiple />
<img id="image" />

<script>
    function onLoad(evt) {
        /* do sth with evt.target.result - it image in base64 format ("data:image/jpeg;base64,....") */
        localStorage.setItem('image', evt.target.result);
        document.getElementById('image').src = evt.target.result;
    };

    function handleFileUpload(evt) {
        var files = evt.target.files; // FileList object

        for (var i = 0; i < files.length; i++) {
            var f = files[i];

            // Only process image files.
            if (!f.type.match('image.*')) {
                continue;
            }

            var reader = new FileReader();
            reader.onload = onLoad;
            reader.readAsDataURL(f);
        }
    }

    document.getElementById('files').addEventListener('change', handleFileUpload, false);

    var image = localStorage.getItem('image');
    if (image !== null) {
        document.getElementById('image').src = image;
    }
</script>

Yeah absolutely, providing you have a cutting edge browser (Chrome or Firefox). 是的,绝对可以,只要您拥有最先进的浏览器(Chrome或Firefox)。

You want to use FileReader and readAsDataURL() . 您要使用FileReaderreadAsDataURL() Take a look at the third example in this tutorial: 看一下本教程中的第三个示例:

http://www.html5rocks.com/en/tutorials/file/dndfiles/ http://www.html5rocks.com/en/tutorials/file/dndfiles/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将动态图像加载到网页中? - How to load a dynamic image into a web page? 可以在没有网络服务器的情况下将网页用作以python编写的程序的用户界面,该程序在本地运行吗? - Is it possible to use a web page as a user interface to a program written in python, running locally, without a web server? 如何在本地保存网页中的文本值,并且网页前端,C#后端是否可能? - How to save text values from web page locally and is it possible to have web page front end, C# back end? JS:从Web加载页面内容(如果可能),回退到本地副本 - JS: Load page content from web (if possible), fallback to local copy 是否可以通过AJAX加载跨域网页? - Is it possible to load a cross domain web page through AJAX? 网页:当我在本地运行时,某些图像会出现,但在Internet上显示时,它们会消失 - Web Page: Certain image appears when I run locally, but disappears when displayed on the internet 是否可以在加载后在页面的其他部分加载后克隆图像? - Is it possible to clone an image after load in other parts of the page without loading? 如果条件为真,是否可以加载新网页(单击按钮),否则在同一网页上执行某些操作? - Is it possible to load a new web-page (on button click) if a conditional is true, otherwise do something on the same web page? tinymce加载图像列表已在网页上提供 - tinymce load image list already available on the web page 如何检测在 web 页面上未正确加载/未完成的图像? - How to detect the image that not load correctly/completed on web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM