描述和代码:
1)我的网站上有这样的图像文件(由动态jQuery上传创建):
<img src="blob:http%3A//www.example.net/6cf3e4f6-9666-41c8-a588-73f2e5057ee0">
2)在执行某些操作(例如单击按钮)之后,我要将所有这些BLOB图像保存在服务器上。我使用jQuery代码:
$('button').on('click', function() {
$('#editor-label-working-area .object_container .image_container img').each(function() {
var blob = null;
var image = $(this);
var file = $(image).attr('src');
var oReq = new XMLHttpRequest();
oReq.open('GET', file, true);
oReq.responseType = 'blob';
oReq.onload = function() {
blob = new Blob([oReq.response], {
type: 'image/jpg'
});
};
oReq.send();
//(Problem No. 1) This timeOut is used just for test. I need to wait untill XMLHttpRequest is finished.
setTimeout(function() {
var formData = new FormData();
formData.append('file', blob);
$.ajax({
type: 'POST',
url: 'upload_image.php',
data: formData,
processData: false,
contentType: false,
success: function(data) {
console.log(data);
},
error: function(response) {
console.log(response);
}
});
}, 2000);
});
});
3)在AJAX请求上,我想使用我的PHP代码来获取发布的文件并将其保存在服务器上:
<?php
define('UPLOAD_DIR', 'labels/');
/*(Problem No. 2) Here I get empty array from $_POST method*/
$img = $_POST['file'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.jpg';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
问题
1.可选问题:在我的jQuery代码中,我必须使用JS超时方法来等待XMLHttpRequest创建blob文件。我需要了解如何使用某种等待方式,而不是随机等待秒,
2)主要也是最重要的问题:为什么我的PHP代码在$ _POST方法中收到空数组?如果我做
echo json_encode($_POST['file']);
..,然后AJAX .success函数在我的浏览器控制台中打印一个空数组:
[]
参考方案
除了使用setTimeout之外,只需将ajax请求放在第一个请求的成功位置即可。
附加Blob时,数据不会存储在$ _POST中,而是存储在$ _FILES中。
var image = $(this);
var file = $(image).attr('src');
var oReq = new XMLHttpRequest();
oReq.open('GET', file, true);
oReq.responseType = 'blob';
oReq.onload = function() {
var formData = new FormData();
formData.append('file', this.response);
$.ajax({
type: 'POST',
url: 'upload_image.php',
data: formData,
processData: false,
contentType: false,
success: function(data) {
console.log(data);
},
error: function(response) {
console.log(response);
}
});
};
oReq.send();
PHP strtotime困境 - php有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00…
PHP-全局变量的性能和内存问题 - php假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…
PHP-从最后一个循环中删除逗号 - php我在循环时有一个PHP,如果是最后一个循环,我想从,中删除最后一个逗号echo '],'; while($ltr = mysql_fetch_array($lt)){ echo '['; echo $ltr['days']. ' ,'. $ltr['name…
PHP Laravel从另一个驱动器读取文件 - php我目前正在学习Laravel。我想知道是否有一种方法可以在Laravel中使用Storage::从另一个硬盘访问文件(使用Windows)。例如,我在驱动器C:上安装了带有Laravel的Xampp,但是我想访问网站目录之外的E:上的文件。我试过使用Storage::files('E:')和File::files('E:…