简体   繁体   English

通过AJAX发送文件返回“ blob”错误

[英]Sending file through AJAX returns 'blob' error

I'm trying to make an AJAX request send a file towards my controller, which then should proceed to read out the sent file and return a sorted array back to the front-end, though it doesn't work as I'm stuck on a 'blob' not set error. 我试图使AJAX请求向控制器发送文件,然后控制器应继续读取已发送的文件,并将排序后的数组返回前端,尽管由于卡住而无法正常工作“ blob”未设置错误。 I've searched about but didn't really find the answer I was looking for, thus why I'm creating this question. 我进行了搜索,但没有真正找到所需的答案,因此为什么创建此问题。

Currently I'm receiving the following TypeError: 目前,我收到以下TypeError:

'slice' called on an object that does not implement interface Blob.

The data that I'm trying to send is as following: 我要发送的数据如下:

http://piclair.com/qk8ms (pic because I can't copy the data directly from my browser.) http://piclair.com/qk8ms (之所以如此,是因为我无法直接从浏览器中复制数据。)

The javascript: JavaScript:

// Click handler for file read
$(document).on('click','#load-csv-btn',function(e){
    e.preventDefault();
    var file = $('#file_upload')[0].files;
    console.log(file);

    readCSV(file);
});


function readCSV(file){
    $.ajax({
        type: "POST",
        url: "ajax/read.php",
        data: {file: file},
        contentType: false
    }).success(function(data){
        console.log(data);
    }).fail(function(){
        console.log('error');
    });
}

The PHP handler, though I guess that part is kind of irrelevant for this problem: PHP处理程序,尽管我猜这部分与该问题无关:

<?php
    var_dump($_POST);

I've found someone saying that setting processData: false and contentType: false would let the script ignore the blob requirment, but if I set those parameters my server just sends back an empty array, which I guess is no suprise as I'm not processing any data. 我发现有人说设置processData: falsecontentType: false将使脚本忽略blob要求,但是如果我设置了这些参数,我的服务器只会发回一个空数组,我猜这不是我的惊奇,因为我不是处理任何数据。

So I'm hoping someone knows a solution to this problem and is able to help me out. 因此,我希望有人知道该问题的解决方案并能够为我提供帮助。

EDIT: 编辑:

Just to make clear which method I've tried that returns an empty array, here is the alternate code: 只是为了弄清楚我尝试过哪种方法返回空数组,下面是替代代码:

$(document).on('click','#load-csv-btn',function(e){
    e.preventDefault();
    var file = $('#file_upload')[0].files[0];
    var fd = new FormData();
    fd.append("csv",file);

    readCSV(fd);
});


function readCSV(file){
    $.ajax({
        type: "POST",
        url: "ajax/read.php",
        data: {file: file},
        contentType: false,
        processData: false
    }).success(function(data){
        console.log(data);
    }).fail(function(){
        console.log('error');
    });
}

As far as I am concerned AJAX uploads are not easily done without external libraries. 就我而言,没有外部库就很难完成AJAX上传。 At least, that is what I learned the first time I needed this done. 至少,这是我第一次需要这样做的经验。

http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html

This guide is pretty good, but a little bit outdated if you are using a newer version of jQuery. 该指南相当不错,但是如果您使用的是jQuery的较新版本,则该指南会有些过时。 You might need to change some deprecated functionality such as ( .live() to .on() ). 您可能需要更改一些不推荐使用的功能,例如( .live().on() )。

In the end I can warmly recommend it as a good start. 最后,我可以热烈推荐它作为一个好的开始。 I've used it widely for years and with great success. 我已经使用了很多年,并且取得了巨大的成功。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM