【发布时间】: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