Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blueimp jQuery文件上传插件-“;空文件上传";结果PHP_Php_Jquery_Jquery Plugins_Blueimp - Fatal编程技术网

Blueimp jQuery文件上传插件-“;空文件上传";结果PHP

Blueimp jQuery文件上传插件-“;空文件上传";结果PHP,php,jquery,jquery-plugins,blueimp,Php,Jquery,Jquery Plugins,Blueimp,以下是插件: 上传文件后,我无法从插件中获得所需的响应 在插件页面上,我有以下内容 $('#fileupload').fileupload( 'option', { 'maxNumberOfFiles' :1, 'url' : '/admin/upload_handler.php' } ); 在upload\u handler.php中,我成功地从$\u文件中检索了上传的文件并执行了一些操作,然后用JSON返回一个响应。我已使用Fireb

以下是插件:

上传文件后,我无法从插件中获得所需的响应

在插件页面上,我有以下内容

$('#fileupload').fileupload(
    'option',
    {
        'maxNumberOfFiles' :1,
        'url' : '/admin/upload_handler.php'
    }
);
upload\u handler.php
中,我成功地从$\u文件中检索了上传的文件并执行了一些操作,然后用JSON返回一个响应。我已使用Firebug确认响应的格式正确:

[ 
    {                
        "url" : "image_url",
        "thumbnail_url" : "image_th_url",
         "delete_url" : "test",
         "delete_type" : "DELETE",
         "name" : "foobar.jpg",
         "size" : 7419
     }
]

但是回调找不到files数组,我得到了错误:“Empty file upload result”。我觉得我遗漏了一些关键的东西——我在文档、论坛或堆栈溢出中找不到任何东西。非常感谢您的帮助。

好的,这似乎不是jQuery的问题,而是服务器端的问题。在我的例子中,它是一个PHP脚本,需要进行如下调整

请参见函数post(),编辑此行

$json = json_encode($info);

我还必须编辑函数最后一行的echo;而不是

echo $json;
现在有了

echo str_replace('\/','/',$json);
返回正确的json数组似乎效果不错。希望有帮助。

将HTML(以及在编程结果DOM中)对象添加到HTML标记之外的模板时,会出现“空文件上载结果”,例如:假设您向模板添加了另一个对象

这将导致正确上载文件,并且对于每个上载的文件,您将获得一次“空文件上载结果”

似乎与JS模板引擎有关

这可以在jquery.fileupload ui js中修复,就在第128行之后

原始代码:

    // Callback for successful uploads:
done: function (e, data) {
    var that = $(this).data('blueimp-fileupload') ||
            $(this).data('fileupload'),
        files = that._getFilesFromResponse(data),
        template,
        deferred;
    if (data.context) {
        data.context.each(function (index) { // LINE 128
            var file = files[index] ||
                    {error: 'Empty file upload result'},
                deferred = that._addFinishedDeferreds();
在第128行之后添加以下代码:

if (!$(data.context[index]).hasClass(that.options.uploadTemplateId)) { return true; }
结果代码:

    // Callback for successful uploads:
done: function (e, data) {
    var that = $(this).data('blueimp-fileupload') ||
            $(this).data('fileupload'),
        files = that._getFilesFromResponse(data),
        template,
        deferred;
    if (data.context) {
        data.context.each(function (index) { //LINE 128
            if (!$(data.context[index]).hasClass(that.options.uploadTemplateId)) { return true; } // YOUR ADDED LINE
            var file = files[index] ||
                    {error: 'Empty file upload result'},
                deferred = that._addFinishedDeferreds();

希望这对其他人有所帮助:)

自从插件版本5以来,json响应已经发生了变化:

因此,您只需使用以下工具调整upload类:

$filejson = new stdClass();
$filejson->files[] = $fileArray;
return json_encode($filejson);

您已经完成了

您的报税表需要包含在一个文件数组中,如下所示:

    {"files": [
      {
        "name": "picture1.jpg",
        "size": 902604,
        "url": "http:\/\/example.org\/files\/picture1.jpg",
        "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture1.jpg",
        "deleteUrl": "http:\/\/example.org\/files\/picture1.jpg",
        "deleteType": "DELETE"
      },
      {
        "name": "picture2.jpg",
        "size": 841946,
        "url": "http:\/\/example.org\/files\/picture2.jpg",
        "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture2.jpg",
        "deleteUrl": "http:\/\/example.org\/files\/picture2.jpg",
        "deleteType": "DELETE"
      }
    ]}
特别是,要求是:
注意,响应应该始终是包含文件数组的JSON对象,即使只上载了一个文件。


via::

在我的例子I中,我修改了jquery.fileupload-ui.js文件以直接查找JSON结果

更改此代码段

if(data.context){
data.context.each(函数(索引){
var file=文件[索引]||
{错误:“空文件上载结果”};//更改
延迟=延迟。_addFinishedFerreds();
那个._转换($(这个))。完成(

函数(){
,正如前面提到的,这是因为服务器响应不是插件所期望的(它是一个文件数组)。 如果您不想(或不能)更改后端,解决方案是将响应中的结果对象替换为插件期望的结果对象(然后包含文件数组)

这可以在fileuploaddone事件中完成

假设这是上传完成后服务器返回的JSON响应:

数据。结果保存服务器响应:

.bind('fileuploaddone', function(e, data) {
    //this will now contains the server response 
    //as per the attached image: an array of elements
    data.result;
});
我们想用一个新的对象替换结果对象,这个对象可以被blueimp插件解析,让我们来定义它(注意:根据插件文档,这是一个包含文件数组的对象)

替换结果对象:

.bind('fileuploaddone', function(e, data) {
    //creating a file object in the format the jquery plugin is expecting
    var file = {
        deleteType: "DELETE",
        deleteUrl:"#",
        type: data.result[0].MimeType,
        thumbnailUrl : "#",
        url: "#",
        name: data.result[0].Name,
        size: data.result[0].Size
    }

    //adding it to the file list
    filesUploaded.files.push(file);
    data.result = null;
    //replacing the server response with the 'custom' one we just created
    data.result = filesUploaded;
});

插件现在应该呈现良好状态,因为它将解析预期的JSON模式,您将不会再收到“空文件上载结果”消息。

这方面有任何进展吗?我有类似的问题(w/CodeIgniter设置)。还没有,它暂时被搁置。如果再次尝试,我肯定会在这里更新。
//tempFolder is optional
var filesUploaded = { "files": [], "tempFolder": null };
.bind('fileuploaddone', function(e, data) {
    //creating a file object in the format the jquery plugin is expecting
    var file = {
        deleteType: "DELETE",
        deleteUrl:"#",
        type: data.result[0].MimeType,
        thumbnailUrl : "#",
        url: "#",
        name: data.result[0].Name,
        size: data.result[0].Size
    }

    //adding it to the file list
    filesUploaded.files.push(file);
    data.result = null;
    //replacing the server response with the 'custom' one we just created
    data.result = filesUploaded;
});