【问题标题】:jQuery Drag-and-Drop + click Image UploadjQuery拖放+点击图片上传
【发布时间】:2020-10-21 15:39:40
【问题描述】:

我想要一个简单的拖放区来通过 AJAX 和 jQuery 上传图片。我找到了一些插件,但是它们对于需要的东西来说太定制了,我无法让它们中的任何一个正常工作。 我还希望拖放区可以点击,以便从操作系统文件对话框中手动选择文件。

我找到了这个脚本,它工作正常,但放置区域不可点击:

// ---------------------------- drop zone to upload image : '#dropfile'
$(document).on('dragenter', '#dropfile', function() {
            return false;
});
 
$(document).on('dragover', '#dropfile', function(e){
            e.preventDefault();
            e.stopPropagation();
            return false;
});
 
$(document).on('dragleave', '#dropfile', function(e) {
            e.preventDefault();
            e.stopPropagation();
            return false;
});

$(document).on('drop', '#dropfile', function(e) {
            if(e.originalEvent.dataTransfer){
                       if(e.originalEvent.dataTransfer.files.length) {
                                   // Stop the propagation of the event
                                   e.preventDefault();
                                   e.stopPropagation();
                                   // Main function to upload
                                   upload(e.originalEvent.dataTransfer.files);
                       }
            }
            return false;
});

function upload(files) {
            var f = files[0] ;
 
            // Only process image files.
            if (!f.type.match('image/jpeg')) {
                       alert(‘The file must be a jpeg image’) ;
                       return false ;
            }
            var reader = new FileReader();
            // When the image is loaded, run handleReaderLoad function
            reader.onload = handleReaderLoad;
            // Read in the image file as a data URL.
            reader.readAsDataURL(f);            
}

function handleReaderLoad(evt) {
            var pic = {};
            pic.file = evt.target.result.split(',')[1];
            var str = jQuery.param(pic);
            $.ajax({
                       type: 'POST',
                       url: ‘url_to_php_script.php’,
                       data: str,
                       success: function(data) {
                                   //do_something(data) ;
                       }
            });
}

所以我添加了一个不可见的文件类型输入,但图像数据似乎发送了两次。我想这是由于原始放置区的不良事件传播:

// ---------------------------- clickable drop zone with invisible file input '#inputFile'
$('#dropfile).on('click', function() {
    $('input#inputFile').trigger('click');
    $('input#inputFile').change(function(e) {
        upload($('input#inputFile')[0].files);
    });
});

我尝试添加这些行,但数据总是发送两次:

$('#dropfile).on('click', function() {
    $('input#inputFile').trigger('click');
    $('input#inputFile').change(function(e) {
        upload($('input#inputFile')[0].files);
        // -------------- stop sending data twice ???
        e.preventDefault();
        e.stopPropagation();
        return false;
    });
});

【问题讨论】:

    标签: javascript jquery ajax file-upload upload


    【解决方案1】:

    我仍然不知道为什么数据会发送两次,但我在这里找到了更好的脚本: https://makitweb.com/drag-and-drop-file-upload-with-jquery-and-ajax/

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 1970-01-01
      • 2018-03-22
      • 2017-05-21
      • 2011-12-01
      • 2017-12-23
      • 2016-08-19
      • 2011-06-23
      • 2012-09-08
      相关资源
      最近更新 更多