Javascript 接收附加到FormData的blob时,pathInfo()的行为异常
我有点困惑,这是一种预期的行为吗 Javascript-示例01Javascript 接收附加到FormData的blob时,pathInfo()的行为异常,javascript,php,ajax,image-processing,canvas,Javascript,Php,Ajax,Image Processing,Canvas,我有点困惑,这是一种预期的行为吗 Javascript-示例01 function convertCanvasToImage() { var temp_ctx, temp_canvas; temp_canvas = document.createElement('canvas'); temp_ctx = temp_canvas.getContext('2d');
function convertCanvasToImage() {
var temp_ctx, temp_canvas;
temp_canvas = document.createElement('canvas');
temp_ctx = temp_canvas.getContext('2d');
temp_canvas.width = windowWidth;
temp_canvas.height = windowWidth;
temp_ctx.drawImage(ctx.canvas, cutoutWidth, cutoutWidth, windowWidth, windowWidth, 0, 0, windowWidth, windowWidth);
var multiPart = new FormData();
temp_canvas.toBlob(function (blob) {
//Tilføjer blob til form objektet
multiPart.append('pernille', blob, ".jpg");
//Ajax kaldet
var http = new XMLHttpRequest();
var url = "ajax.php";
http.open("POST", url, true);
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
alert(this.responseText);
console.log(this.responseText);
}
};
http.send(multiPart);
}, "image/jpeg");
}
<pre>$_POSTArray
(
)
$_FILESArray
(
[name] => pernille
[type] => image/jpeg
[tmp_name] => /home/xch07.wi2/tmp/phpaKQwU4
[error] => 0
[size] => 17601
)
pathinfoArray
(
[dirname] => .
[basename] => pernille
[filename] => pernille
)
</pre>
Javascript-示例02
function convertCanvasToImage() {
var temp_ctx, temp_canvas;
temp_canvas = document.createElement('canvas');
temp_ctx = temp_canvas.getContext('2d');
temp_canvas.width = windowWidth;
temp_canvas.height = windowWidth;
temp_ctx.drawImage(ctx.canvas, cutoutWidth, cutoutWidth, windowWidth, windowWidth, 0, 0, windowWidth, windowWidth);
var multiPart = new FormData();
temp_canvas.toBlob(function (blob) {
//Tilføjer blob til form objektet
multiPart.append('pernille', blob, "pernille");
//Ajax kaldet
var http = new XMLHttpRequest();
var url = "ajax.php";
http.open("POST", url, true);
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
alert(this.responseText);
console.log(this.responseText);
}
};
http.send(multiPart);
}, "image/jpeg");
}
阿贾克斯
Func 02
<pre>$_POSTArray
(
)
$_FILESArray
(
[name] => .jpg
[type] => image/jpeg
[tmp_name] => /home/xch07.wi2/tmp/phpZ6zxv2
[error] => 0
[size] => 17713
)
pathinfoArray
(
[dirname] => .
[basename] => .jpg
[extension] => jpg
[filename] =>
)
</pre>
Javascript示例01将从AJAX调用中输出此结果
没有文件名
$\u邮递
(
)
$\u文件数组
(
[名称]=>.jpg
[类型]=>图像/jpeg
[tmp_name]=>/home/xch07.wi2/tmp/phpZ6zxv2
[错误]=>0
[大小]=>17713
)
pathinfoArray
(
[dirname]=>。
[basename]=>.jpg
[扩展]=>jpg
[文件名]=>
)
Javascript示例02将从AJAX调用中输出此文件名,但没有扩展名
$\u邮递
(
)
$\u文件数组
(
[名称]=>pernille
[类型]=>图像/jpeg
[tmp_name]=>/home/xch07.wi2/tmp/phpaKQwU4
[错误]=>0
[大小]=>17601
)
pathinfoArray
(
[dirname]=>。
[basename]=>pernille
[filename]=>pernille
)
这并不是行为怪异,而是您错误地使用了方法
正确的表格应为:
您需要将文件扩展名附加到文件名本身
请参阅了解更多信息。这就解释了很多,谢谢。。真不敢相信我错过了:)
multiPart.append('pernille', blob, "pernille");
<pre>$_POSTArray
(
)
$_FILESArray
(
[name] => .jpg
[type] => image/jpeg
[tmp_name] => /home/xch07.wi2/tmp/phpZ6zxv2
[error] => 0
[size] => 17713
)
pathinfoArray
(
[dirname] => .
[basename] => .jpg
[extension] => jpg
[filename] =>
)
</pre>
<pre>$_POSTArray
(
)
$_FILESArray
(
[name] => pernille
[type] => image/jpeg
[tmp_name] => /home/xch07.wi2/tmp/phpaKQwU4
[error] => 0
[size] => 17601
)
pathinfoArray
(
[dirname] => .
[basename] => pernille
[filename] => pernille
)
</pre>
multiPart.append('pernille', blob, 'pernille.jpeg');
^ ^ ^
field-name data-value file-name