【问题标题】:How to blob a large file, upload it via ajax, display a progress bar and merge the file parts in php?如何blob一个大文件,通过ajax上传,显示进度条并在php中合并文件部分?
【发布时间】:2014-02-19 16:12:42
【问题描述】:

我想编写一个带有表单的客户端脚本,您可以在其中将大文件 (>2GB) 上传到服务器。服务器将无法通过常规的 html 上传来处理如此大的文件,因此必须将文件拆分为多个文件部分。因此我的html表单是:

<form enctype="multipart/form-data" method="post" id="fileinfo" name="fileinfo">
          <label>Your email address:</label>
          <input type="email" autocomplete="on" autofocus name="userid" placeholder="email" required size="32" maxlength="64" /><br />
          <label>Custom file label:</label>
          <input type="text" name="filelabel" size="12" maxlength="32" /><br />
          <label>File to stash:</label>
          <input type="file" name="file" required />
</form>        
<a href="javascript:sendForm()">Stash the file!</a>

jQuery 是:

function sendForm() {
     var fd = new FormData(document.getElementById("fileinfo"));
     fd.append("CustomField", "This is some extra data");
     $.ajax({
        url: "stash.php",
        type: "POST",
        data: fd,
        processData: false,  // tell jQuery not to process the data
        contentType: false,   // tell jQuery not to set contentType
        success: function(response){
             console.log("Response was "  + response);
        },
        failure: function(result){
            console.log("FAILED");
            console.log(result);
        }
    });

};

我的 php 脚本现在真的很短,因为我只是想测试它是否接收到表单数据:

if (isset($_POST['filelabel']) ) { var_dump('发送'); var_dump($_POST); }

但是如何制作“blob”并设置文件部分的大小?以及之后如何进行?任何提示、链接或代码 sn-ps 都会很有用!

【问题讨论】:

  • 我将我的 cmets 移至答案,因为我认为这是实现目标的最佳方式 ;-)

标签: jquery ajax forms file-upload process


【解决方案1】:

我会推荐使用 jquery fileupload 插件....这是获得你想要的最简单的方法https://blueimp.github.io/jQuery-File-Upload

还可以看看这个附加的插件:https://github.com/blueimp/jQuery-File-Upload/wiki/Chunked-file-uploads

【讨论】:

  • 天哪! jQuery-File-Upload 的文档和教程确实令人困惑。有一个有效的示例,但很难自定义上传器。很多人似乎对它有问题:stackoverflow.com/questions/17934689/… 和教程缺少代码:htmlgoodies.com/html5/other/… 堆栈溢出问题的作者推荐 Uploadify 但它不是免费工具... :(
  • 是的,配置挺难的,我在一个项目中用过一次,有点麻烦。虽然我仍然认为这是最简单的方法(请记住,您自己的解决方案必须在多个浏览器和连接等中测试证明其工作,等等,这也不是一件容易的工作)祝你好运!
猜你喜欢
  • 2010-11-01
  • 2011-07-01
  • 2013-01-26
  • 2018-12-07
  • 1970-01-01
  • 2013-02-27
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多