【问题标题】:Drag-and-drop file upload in Google Chrome/Chromium and Safari?在 Google Chrome/Chromium 和 Safari 中拖放文件上传?
【发布时间】:2011-02-09 02:27:18
【问题描述】:

在 Firefox 3.6 中可以进行拖放文件上传。

A Google search for html5 drag-and-drop file uploading -gmail 给出如下内容:

所有这些指南都使用 FileReader(或 Firefox 3.6 已弃用的 getAsBinary,其他浏览器也不支持)。

不过,Google 最近发布了 Gmail 更新,允许在 Chromium 和 Firefox 和 Chromium does not have FileReader 中拖放文件上传。我用的是最新的Chromium nightly,可以拖拽上传文件,但不支持FileReader

我看到有人提到可以通过拖放到<input type="file" /> 上进行拖放上传,但是一次只能支持一个文件,而 Gmail 的上传器可以处理多个文件被拖放到上面,所以这是显然不是他们在做什么。

所以问题是,他们是如何做到的?您如何支持 Chromium 进行 HTML5 文件上传?另外,能支持Safari吗?

【问题讨论】:

  • P.S.阅读源代码非常困难,因为 Gmail 的源代码由于他们对其进行的所有优化而被很好地混淆了。
  • P.P.S. HTML <input type="file" />FileList 是只读的,因此您也不能将拖放的文件移植到其中一个。
  • 这篇文章的标题可以改吗?我刚刚发布了一个重复的问题,询问“Chrome”中的“拖放”,但它与这个问题不匹配,因为标题使用 Chromium(大多数人不使用)。
  • 好的,我改了标题。

标签: javascript ajax html


【解决方案1】:

警告:这是非常旧版本的 Safari 和 Chrome 的兼容性代码。现代浏览器都支持 FileReader API;这是一个教程:https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

此代码现在仅在出于某种原因需要支持 Safari 5 及更早版本或 Chrome 6 及更早版本时才有用。


一种可能是使用the method used in SwellJS

像这样使用<input type="file" multiple="multiple" />

<form method="post" enctype="multipart/form-data" id="uploadform">
  <input type="file" name="dragupload[]" multiple="multiple"
  onchange="if (this.value) document.getElementById('uploadform').submit();" />
</form>

可以将输入元素设置为具有opacity: 0 的样式,并(绝对)定位在接受上传的元素上。整个表单可以放在iframe 中,用于类似“伪 Ajax”的行为。上传元素可以是隐藏的层,直到有东西被拖到它上面。

这样的 iframe 看起来像:

<script>
<!--
  var entered = 0;
-->
</script>
<body ondragenter="entered++;document.getElementById('uploadelement').style.display='block'" ondragleave="entered--;if (!entered) document.getElementById('uploadelement').style.display='none'">
  <form method="post" enctype="multipart/form-data" id="uploadform">
    Things can be dragged and dropped here!
    <input type="file" id="uploadelement" name="dragupload[]" multiple="multiple" onchange="if (this.value) { document.getElementById('uploadform').submit(); }" style="display:none;position:absolute;top:0;left:0;right:0;bottom:0;opacity:0;" />
  </form>
</body>

只有在检测到 Safari 或 Chrome 时才应该这样做(因为其他浏览器不支持拖放到 &lt;input type="file" /&gt; 元素上),并且可以与 Firefox 3.6 的 HTML5 drop 事件结合使用+。

我不知道这是否是 Gmail 使用的方法,但它确实也可以。

【讨论】:

  • 显示/隐藏代码中的条目计数递增/递减技巧非常方便。
  • 感谢您的这篇文章。您是否可以从此处链接到完整示例?一个可能包括服务器端代码?或者只是解释(在 asp.net 上)我如何访问从这个表单(“uploadForm”)提交的文件
【解决方案2】:

您可能对更符合技术和浏览器的东西感兴趣。

在我看来Plupload 做得很好,支持以下功能:

  • 分块
  • 拖放
  • PNG 调整大小
  • JPEG 调整大小
  • 类型过滤
  • 流上传
  • 分段上传
  • 文件大小限制
  • 上传进度

适用于以下大多数技术:

  • 闪光
  • 齿轮
  • HTML 5
  • 银光
  • BrowserPlus

是的,since 2010.05.27,它支持在 Chrome beta 上运行的 HTML5 拖放。

【讨论】:

  • 哦?您是否尝试过此页面的“HTML 5 运行时”? plupload.com/example_all_runtimes.php
  • HTML5 运行时版本适用于 OS X 上的 6.0.472.25 dev。它似乎也适用于 Firefox 4 测试版。
  • HTML5 上传也适用于我在 WinXp 上的 Chrome 8.552.210 Beta 上(仅几个月,他们有 2 个版本)。无论如何感谢您的帖子!
  • 对我来说:在 html5 运行时,FF 15.0.1 有效; Chrome 21 没有。
【解决方案3】:

经过大量、大量、大量的侦探工作,我在 Chrome 中找到了一些东西。这适用于 Chrome。在 Safari 上,它会冻结。在 Firefox 上,它不会让我删除文件。 IE 会打开删除的文件。即使在 Chrome 中,由于某种原因,拖放也只能工作一次,之后您必须刷新页面。 (一个可能的原因是事件处理程序有问题。)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
            window.onload = function () {
                var div = document.getElementById('div');
                div.ondragenter = div.ondragover = function (e) {
                    e.preventDefault();
                    e.dataTransfer.dropEffect = 'copy';
                    return false;
                }
                div.ondrop = function (e) {
                    for (var i = 0; i < e.dataTransfer.files.length; i++) { // e.dataTransfer is a DataTransfer object (https://developer.mozilla.org/En/DragDrop/DataTransfer), e.dataTransfer.files is a FileList object (https://developer.mozilla.org/en/DOM/FileList)
                        var file = e.dataTransfer.files[i]; // file is a File object (https://developer.mozilla.org/en/DOM/File)

                        var xhr = new XMLHttpRequest;
                        xhr.open('post', 'handler.php', true);
                        xhr.onreadystatechange = function () {
                            if (this.readyState != 4)
                                return;
                            document.body.innerHTML += '<pre>' + this.responseText + '</pre>';
                        }
                        xhr.setRequestHeader('Content-Type', 'multipart/form-data');
                        xhr.setRequestHeader('X-File-Name', file.fileName);
                        xhr.setRequestHeader('X-File-Size', file.fileSize);
                        xhr.send(file); // For some reason sending the actual File object in Chrome works?
                    }

                    e.preventDefault();
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <div id="div" style="width: 100%; height: 200px; border: 1px solid blue">Drop here</div>
    </body>
</html>

handler.php:

    // This is not a true file upload. Instead, it sends the raw data directly.
    echo htmlentities(file_get_contents('php://input'));

【讨论】:

  • 使用 document.body.innerHTML += 会破坏 dom 事件。避免这种情况将解决您的问题。
  • 效果很好,一个 gmail 开发人员告诉我,你应该对 FF3.6+ 使用 dropEffect='move',还没有在 FF 中测试过,所以不确定这是否仍然需要
【解决方案4】:

您不需要使用 iframe 来进行伪 ajax 上传。 Chrome 和 Safari 都支持 XHR2 uploads 和进度事件,所以你可以做进度条等。

【讨论】:

  • ...是的,这很明显。这里的问题是我见过的所有教程都使用FileReadergetAsBinary()(即Chrome 不支持的功能)来XHR2 上传文件。我怀疑这是必需的,所以我在这里基本上要做的是询问如果想要 Chrome 支持该怎么做。
【解决方案5】:

对于我们自己的应用程序,我们只对 FireFox 进行拖放。我们恢复为其他人的传统 iframe 上传。为了检测是否支持拖放,我们运行以下代码:

if (typeof(window.File) == 'object' && typeof(window.FileReader) == 'function' && typeof(window.FileList) == 'object') {
   // DnD is supported!
}

希望这对某些人有所帮助。

【讨论】:

  • 您确实提到您仅对 FireFox 进行拖放,但对于可能使用此代码的其他人,Safari 5 (Windows) 将为 window.FileReader 检查返回 false,即使它处理文件滴得很好。仍在为文件拖放寻找可靠的浏览器功能检测...
【解决方案6】:

您可以使用 html5uploader 库:http://code.google.com/p/html5uploader/

它适用于 Firefox、Safari 和 Chrome。

【讨论】:

    【解决方案7】:

    最新浏览器支持文件上传。 你可以使用:

    xhr = new XMLHttpRequest();     
    xhr.open('POST', targetPHP, true);
    var formData = new FormData();
    formData.append('upload',file);
    xhr.send(formData);
    

    你不需要设置边界或任何头部,这样就可以了。 我在 客户端:firefox 6.02 和 chrome 13。 服务器:带有“spring mvc”的tomcat

    【讨论】:

      【解决方案8】:

      您可以使用 FormData 来存储文件,然后上传。例如

      function setUp(){
        var dropContainer = document.getElementById("container");
        dropContainer.addEventListener("drop",dropHandler,false);
        dropContainer.addEventListener("dragenter", function(event){event.stopPropagation();event.preventDefault();}, false);
        dropContainer.addEventListener("dragover", function(event){event.stopPropagation();event.preventDefault();}, false);
        dropContainer.addEventListener("drop", dropHandler, false);
        getResult()
      }
      function dropHandler(event){
        var files = event.dataTransfer.files;
        var count = files.length;
        form = new FormData();
        for(var i= 0;i<count;i++){
          form.append("file"+i, files[i]);
        }
        sendData();
      }
      function sendData(){
        var xhr = new XMLHttpRequest();  
        xhr.upload.addEventListener("progress", uploadProgress, false);  
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);  
        xhr.open("POST", "/upload");
        xhr.send(form);
        var progressBar = document.getElementById('progressBar');
        progressBar.style.display = 'block';
        progressBar.style.width = '0px';
      }
      

      演示在这里(http://flexinnerp.appspot.com/) 尽情享受吧:)

      【讨论】:

        【解决方案9】:

        设置多个属性如:

        input type="file" name="file1" multiple="multiple" class="DropHere"

        并使用这个 CSS DropHere 类:

        .DropHere
        {
            height: 100px;
            padding: 3px;
            border: 2px dashed #555;
            border-radius: 5px;
            cursor: default;
            background-image:url("data:image/svg+xml;utf8, <svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='100px' width='220px'><text x='55' y='75' font-size='20'>or drop files here</text></svg>");
            background-repeat: no-repeat;
        }
        

        文件字段现在看起来像:

        如果你使用asp.net你可能也会喜欢我写的这篇文章“带进度条和拖放的多文件上传”:http://www.codeproject.com/Articles/818561/Multiple-file-upload-with-progress-bar-and-drag-an

        【讨论】:

          猜你喜欢
          • 2013-10-31
          • 2017-06-09
          • 2013-01-27
          • 1970-01-01
          • 2011-09-08
          • 2010-11-04
          • 1970-01-01
          相关资源
          最近更新 更多