【问题标题】:drag drop images input file and preview before upload [duplicate]上传前拖放图像输入文件和预览[重复]
【发布时间】:2014-08-02 08:01:28
【问题描述】:

我想创建一个 div 附加拖放功能,当有人点击它时,他们可以选择他们的图像。

我已经编码了一些东西,它可以 - 单击 div 并选择您的图像 - 上传前预览图片

你可以看看我的小提琴

css

.uploader {width:300px;height:350px;background:#f3f3f3;border:2px dashed #e8e8e8;}

javascript

    var imageLoader = document.getElementById('filePhoto');
    imageLoader.addEventListener('change', handleImage, false);

function handleImage(e) {
    var reader = new FileReader();
    reader.onload = function (event) {

        $('.uploader').html( '<img width="300px" height="350px" src="'+event.target.result+'"/>' );
    }
    reader.readAsDataURL(e.target.files[0]);
}

html

<div class="uploader" onclick="$('#filePhoto').click()">click here or drag here your images for preview and set userprofile_picture data</div>
            <input type="file" name="userprofile_picture"  id="filePhoto" style="display:block;width:185px;"  />

你可以检查 http://jsfiddle.net/ELcf6/

【问题讨论】:

    标签: javascript php jquery drag-and-drop


    【解决方案1】:

    您可以使用一些小的 CSS 技巧来做到这一点。 输入元素接受丢弃。 我更改了您的代码,如下所示:

    css

    .uploader {
        position:relative; 
        overflow:hidden; 
        width:300px; 
        height:350px;
        background:#f3f3f3; 
        border:2px dashed #e8e8e8;
    }
    
    #filePhoto{
        position:absolute;
        width:300px;
        height:400px;
        top:-50px;
        left:0;
        z-index:2;
        opacity:0;
        cursor:pointer;
    }
    
    .uploader img{
        position:absolute;
        width:302px;
        height:352px;
        top:-1px;
        left:-1px;
        z-index:1;
        border:none;
    }
    

    javascript

    var imageLoader = document.getElementById('filePhoto');
    imageLoader.addEventListener('change', handleImage, false);
    
    function handleImage(e) {
        var reader = new FileReader();
        reader.onload = function (event) {
            $('.uploader img').attr('src',event.target.result);
        }
        reader.readAsDataURL(e.target.files[0]);
    }
    
    // Edit for multiple images
    // I didn't try but it should work.
    // Also you need write some CSS code to see all images in container properly.
    function handleImages(e) {
        $('.uploader img').remove();
        for(var i = 0; i < e.target.files.length; i++){
            var reader = new FileReader();
            reader.onload = function (event) {
                var $img = $('<img/>');
                $img.attr('src', event.target.result);
                $('.uploader').append($img);
            }
            reader.readAsDataURL(e.target.files[i]);
        }
    }
    

    html

    <div class="uploader" onclick="$('#filePhoto').click()">
        click here or drag here your images for preview and set userprofile_picture data
        <img src=""/>
        <input type="file" name="userprofile_picture"  id="filePhoto" />
    </div>
    

    我希望它有所帮助。可以查看http://jsfiddle.net/ELcf6/4/,其他版本http://jsfiddle.net/ELcf6/8/

    【讨论】:

    • 感谢它的工作!但我想知道你能告诉我我们如何接受输入的细节吗?我看了你的代码,但我不明白哪个技巧可以做到这一点?
    • input 元素通过拖放接受文件,但该属性支持一些浏览器,如 chrome、firefox 等。我将 input 元素放在您的上传器 div 中,并将输入元素的宽度和高度设置为等于上传器 div 跨度>
    • 谢谢你,它在 Firefox 55.0.2 中不起作用。 imageLoader.files 在 Firefox 中的拖放事件中没有改变。
    • @EnginÜstün 我尝试了您的示例,它在更改事件上运行良好。但不适用于拖放错误 Cannot assign to 'files' because it is an constant or read-only property.。我正在使用角度 4。
    • 不适用于多张图片,它只显示最后一张。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2018-07-28
    • 2020-12-03
    • 2018-08-12
    • 2013-11-06
    • 1970-01-01
    • 2017-07-09
    • 2017-05-09
    相关资源
    最近更新 更多