通过XMLHttpRequest将原始数据发送到PHP

时间:2015-11-17 17:28:19

标签: php jquery

我正在选择一个文件并通过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);
    }

因此,对于我添加的每个文件,我创建了一个new ChunkedUploader对象,该文件将文件分块为小1MB个文件。 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);
    },

一切正常,但在PHP端,我没有收到任何内容:$_GET$_POST$_FILE。我可以在Firebug中看到原始数据是通过post发送的,有一些乱码数据被发送,我认为它是我刚刚从原始文件中裁剪的一小块。我到处寻找,我无法找到与此案有关的任何内容。

你能指出我做错了什么,因为我不知道。

1 个答案:

答案 0 :(得分:2)

您可能需要file_get_contents('php://input'):这是原始请求正文,而$_POST已经是已解析的表示。

请参阅http://php.net/manual/en/wrappers.php.php#wrappers.php.input