简体   繁体   English

HTML5 JavaScript POST文件上传

[英]HTML5 JavaScript POST file upload

Recently I've been looking into adding a simple HTML5 drag and drop to my already simple php file upload script. 最近,我一直在考虑向我已经很简单的php文件上传脚本中添加一个简单的HTML5拖放。 I have read lots of tutorials and other solutions but I can't seem to understand the whole sending the file to server part. 我已经阅读了许多教程和其他解决方案,但似乎无法理解将文件发送到服务器的整个过程。 From what I understand, XMLHttpRequest will send the data, but it will do it without reloading the page. 据我了解,XMLHttpRequest将发送数据,但是它将执行此操作而无需重新加载页面。 This, I do not want. 这个,我不要。 Currently the script I'm using will take the POST data and produce the file upload output, eg. 目前,我正在使用的脚本将获取POST数据并生成文件上传输出,例如。 server download link, thumbnail if image etc. 服务器下载链接,缩略图(如果有图片等)。

How can I have the drag and drop submit the POST data and either access the upload output or reload the page? 如何拖放提交POST数据并访问上传输出或重新加载页面?

Edit: I'm using the following for the drag and drop: 编辑:我正在使用以下拖放:

    <div id="drop_zone">Drag and drop a file here</div>

    <script>
          function handleFileSelect(evt) {
            evt.stopPropagation();
            evt.preventDefault();

            var files = evt.dataTransfer.files; // FileList object.


            uploadFile(files[0]); //<-- This is where most examples will use XMLHttpRequest to construct form and send data
          }

        function handleDragOver(evt) {
            evt.stopPropagation();
            evt.preventDefault();
            document.getElementById('drop_zone').style.backgroundColor = "#faffa3";
            evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
        }
        function handleDragLeave(evt) {
            document.getElementById('drop_zone').style.backgroundColor = "";
        }

          // Setup the dnd listeners.
          var dropZone = document.getElementById('drop_zone');
          dropZone.addEventListener('dragover', handleDragOver, false);
          dropZone.addEventListener('drop', handleFileSelect, false);
          dropZone.addEventListener('dragleave', handleDragLeave, false);                 
    </script>

If you use JQuery or some other event-capable javascript library you should be able to catch the drag event and submit your form. 如果您使用JQuery或其他具有事件功能的JavaScript库,则应该能够捕获拖动事件并提交表单。

http://docs.jquery.com/UI/API/1.8/Draggable http://docs.jquery.com/UI/API/1.8/可拖动

http://api.jquery.com/on/ http://api.jquery.com/on/

But no XMLHttpRequest involved... 但是不涉及XMLHttpRequest ...

After more reading and searching around I've come to the conclusion that what I originally wanted cannot be done. 经过更多的阅读和搜索后,我得出的结论是我原本想要的东西无法完成。

The solution I am going with is to simply have a large transparent/hidden <input type="file"> covering the drop zone. 我要使用的解决方案是简单地将覆盖拖放区的透明/隐藏<input type="file"> The dropped filename will be read from the input with javascript and displayed in the drop zone. 删除的文件名将使用javascript从输入中读取,并显示在放置区域中。 This of course will rely on the browser to support the drag and drop files into html file inputs. 当然,这将依靠浏览器来支持将文件拖放到html文件输入中。 From there my upload script will pretty much operate as if the user had used the visible file selector. 从那里,我的上载脚本几乎可以像用户使用可见文件选择器一样运行。

Apologies for not being too clear on what I wanted. 很抱歉,对我想要的内容不太清楚。 I wasn't so sure myself. 我不太确定自己。 And thanks for the replies/comments. 并感谢您的答复/评论。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM