[英]Send raw data to PHP via XMLHttpRequest
I am selecting a file and sending it through XMLXttpRequest like this :我正在选择一个文件并通过 XMLXttpRequest 发送它,如下所示:
var upload_form = $('#upload_form'),
file_input = $('#file_input'),
file_list = $('#file_list'),
submit_btn = $('#submit_btn'),
uploaders = [];
file_input.on('change', onFilesSelected);
upload_form.on('submit', onFormSubmit);
/**
* Loops through the selected files, displays their file name and size
* in the file list, and enables the submit button for uploading.
*/
function onFilesSelected(e) {
var files = e.target.files,
file,
list_item,
uploader;
for (var i = 0; i < files.length; i++) {
file = files[i];
uploader = new ChunkedUploader(file);
uploaders.push(uploader);
list_item = $('<li>' + file.name + '(' + file.size.formatBytes() + ') <button>Pause</button></li>').data('uploader', uploader);
file_list.append(list_item);
}
file_list.show();
submit_btn.attr('disabled', false);
}
so for each file that I add, I create a new ChunkedUploader
object, which chunks the file in little 1MB
files.所以对于我添加的每个文件,我都会创建一个
new ChunkedUploader
对象,它将文件new ChunkedUploader
1MB
小文件。 The code for the ChunkedUploader
object is as follows: ChunkedUploader
对象的代码如下:
function ChunkedUploader(file, options) {
if (!this instanceof ChunkedUploader) {
return new ChunkedUploader(file, options);
}
this.file = file;
this.options = $.extend({
url: 'index/upload'
}, options);
this.file_size = this.file.size;
this.chunk_size = (1024 * 100); // 100KB
this.range_start = 0;
this.range_end = this.chunk_size;
if ('mozSlice' in this.file) {
this.slice_method = 'mozSlice';
}
else if ('webkitSlice' in this.file) {
this.slice_method = 'webkitSlice';
}
else {
this.slice_method = 'slice';
}
this.upload_request = new XMLHttpRequest();
this.upload_request.onload = this._onChunkComplete();
}
_upload: function() {
var self = this,
chunk;
// Slight timeout needed here (File read / AJAX readystate conflict?)
setTimeout(function() {
// Prevent range overflow
if (self.range_end > self.file_size) {
self.range_end = self.file_size;
}
chunk = self.file[self.slice_method](self.range_start, self.range_end);
self.upload_request.open('POST', self.options.url, true);
self.upload_request.overrideMimeType('application/octet-stream');
if (self.range_start !== 0) {
self.upload_request.setRequestHeader('Content-Range', 'bytes ' + self.range_start + '-' + self.range_end + '/' + self.file_size);
}
self.upload_request.send(chunk);
}, 200);
},
It all works okay, but on the PHP end I receive nothing through : $_GET
, $_POST
or $_FILE
.一切正常,但在 PHP 端我没有收到任何通过:
$_GET
、 $_POST
或$_FILE
。 I can see in Firebug that raw data is being sent through post
, there are some gibberish data being sent, I presume it's the small chunk that I just cropped from the original file.我可以在 Firebug 中看到原始数据是通过
post
发送post
,有一些乱码数据正在发送,我认为这是我刚刚从原始文件中裁剪的小块。 I have looked everywhere and I can't find anything releated to this case.我到处找,找不到与此案有关的任何内容。
Can you point out what I am doing wrong, because I have to no clue.你能指出我做错了什么,因为我不知道。
You may want to file_get_contents('php://input')
instead: this is the raw request body, whereas $_POST
is already a parsed representation.您可能想要
file_get_contents('php://input')
代替:这是原始请求正文,而$_POST
已经是解析的表示形式。
See http://php.net/manual/en/wrappers.php.php#wrappers.php.input见http://php.net/manual/en/wrappers.php.php#wrappers.php.input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.