请参阅底部的解决方案。
所以我获得了一个.jpg或.png的Blob并将它存储在服务器上我获得了一个psd的blob并且它没有显示错误,错误是" aborted"在萤火虫里。为什么会这样?
请注意,这适用于localhost,但不适用于实时服务器。
var xxx = 0;
function upload(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.open('POST', 'fileupload.php', true);
xhr.onload = function(e) {};
xhr.setRequestHeader ("enctype", "multipart/form-data");
xhr.send(blobOrFile);
xhr.onreadystatechange = function(a) {
//alert("In upload()");
if(xhr.readyState == 4) {
xxx = 0;
}
if(xhr.readyState == 0 || xhr.readyState == 1 || xhr.readyState == 2 || xhr.readyState == 3) {
xxx = 1;
}
};
}
$(document).on("change", "#userfile", (function(f){
var blob = this.files[0];
upload(blob);
}));
表格
<form action="fileupload.php" method="POST" id="myForm" enctype="multipart/form-data">
<input type="hidden" value="<?php echo ini_get("session.upload_progress.name"); ?>" name="<?php echo ini_get("session.upload_progress.name"); ?>">
<input type="file" name="userfile" id="userfile"><br>
<input type="button" id="but" value="Start Upload">
</form>
js xhr发布的文件
<?php
var_dump($HTTP_RAW_POST_DATA);
$a = fopen("a.psd", "a+b");
fwrite($a,$HTTP_RAW_POST_DATA);
var_dump($_POST);
var_dump($_FILES);
fclose($a);
exit();
?>
解决方案 由于服务器仅接受某些内容类型,该文件被拒绝。
var fd = new FormData();
fd.append( 'file', blobOrFile);
var xhr = new XMLHttpRequest();
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.open('POST', 'fileupload.php', true);
xhr.onload = function(e) {};
xhr.setRequestHeader ("enctype", "multipart/form-data");
xhr.send(fd);
xhr.onreadystatechange = function(a) {
//alert("In upload()");
if(xhr.readyState == 4) {
xxx = 0;
}
if(xhr.readyState == 0 || xhr.readyState == 1 || xhr.readyState == 2 || xhr.readyState == 3) {
xxx = 1;
}
};
使用FormData api发送时,这是预先写入的文件。
Content-Disposition: form-data; name="file"; filename="a.psd"
Content-Type: application/octet-stream