【问题标题】:jQuery Drag-and-Drop Image Upload FunctionalityjQuery 拖放图片上传功能
【发布时间】:2012-06-26 03:32:41
【问题描述】:

我正在尝试通过 jQuery 编写我自己的简单 AJAX 图像上传脚本。我找到了一些插件,但它们对于需要的东西来说太定制了,我无法让它们中的任何一个正常工作。

我只是想以某种方式检测用户何时将图像拖放到页面上。从那里我确信上传这些数据并移动到 /cache/ 目录并允许更多选项并不难..

但现在我完全被拖放功能所困扰。从字面上看,我不知道我应该如何处理这个问题。需要什么样的事件处理程序?我需要自定义代码我自己的事件处理程序吗?任何建议都将不胜感激

【问题讨论】:

标签: image jquery file-upload drag-and-drop


【解决方案1】:

可能为时已晚。但你应该结帐http://www.dropzonejs.com/

【讨论】:

    【解决方案2】:

    需要什么样的事件处理程序?

    Drag'n'drop 需要 HTML5 浏览器 - 但现在几乎就是所有这些了。

    我建议不要从头开始,因为需要相当多的代码 - 我非常喜欢这个将它实现为 jQuery 插件的包装器。

    http://www.github.com/weixiyen/jquery-filedrop

    在文档中用 div 类定义一个元素后,您可以初始化它以接受丢弃的文件:

    function fileSetUploadPercent(percent, divID){
    
        var uploadString = "Uploaded " + percent + " %";
    
        $('#'.divID).text(uploadString);
    }
    function fileUploadStarted(index, file, files_count){
    
        var divID = getDivID(index, file);
    
        createFileUploadDiv(divID);     //create the div that will hold the upload status
    
        fileSetUploadPercent(0, divID); //set the upload status to be 0
    }
    
    function  fileUploadUpdate(index, file, currentProgress){
    
        //Logger.log("fileUploadUpdate(index, file, currentProgress)");
    
        var string = "index = " + index + " Uploading file " + file.fileName + " size is " + file.fileSize + " Progress = " + currentProgress;
        $('#status').text(string);
    
        var divID = getDivID(index, file);
        fileSetUploadPercent(currentProgress, divID);
    }
    
    function fileUploadFinished(index, file, json, timeDiff){
    
        var divID = getDivID(index, file);
        fileSetUploadPercent(100, divID);
    
        if(json.status == "OK"){
            createThumbnailDiv(index, file, json.url, json.thumbnailURL);
        }
    }
    
    
    
    function    fileDocOver(event){
        $('#fileDropTarget').css('border', '2px dashed #000000').text("Drop files here");
    }
    $(".fileDrop").filedrop({
    
                fallback_id: 'fallbackFileDrop',
                url: '/api/upload.php',
                //    refresh: 1000,
                paramname: 'fileUpload',
                //    maxfiles: 25,           // Ignored if queuefiles is set > 0
                maxfilesize: 4,         // MB file size limit
                //    queuefiles: 0,          // Max files before queueing (for large volume uploads)
                //    queuewait: 200,         // Queue wait time if full
                //    data: {},
                //    headers: {},
                //    drop: empty,
                //    dragEnter: empty,
                //    dragOver: empty,
                //    dragLeave: empty,
                //    docEnter: empty,
                docOver: fileDocOver,
            //  docLeave: fileDocLeave,
                //  beforeEach: empty,
                //   afterAll: empty,
                //  rename: empty,
                //  error: function(err, file, i) {
                //    alert(err);
                //  },
                uploadStarted: fileUploadStarted,
                uploadFinished: fileUploadFinished,
                progressUpdated: fileUploadUpdate,
                //     speedUpdated
            });
    

    接受上传的网页部分有这个HTML。

    <div class='fileDrop'>
    Upload a file by dragging it.
    <span id='fileDropTarget'/>
    
    </div>
    

    文件放置在外部

    上工作,但最好制作一个漂亮的大目标,上面写着“DROP HERE”,这样用户就不会对他们需要放置文件的位置感到困惑。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 2019-03-17
      • 2017-12-23
      • 2016-08-19
      • 2011-06-23
      • 1970-01-01
      相关资源
      最近更新 更多