Javascript jQuery/PHP:如何正确返回上传文件信息
我正在使用jquery插件jquery.filer,当我在他们的网站上尝试上传他们的示例文件时,它会给我这样一个返回值Javascript jQuery/PHP:如何正确返回上传文件信息,javascript,php,jquery,Javascript,Php,Jquery,我正在使用jquery插件jquery.filer,当我在他们的网站上尝试上传他们的示例文件时,它会给我这样一个返回值 { "files": [ "..\/uploads\/dK079QrL2g.docx" ], "metas": [ { "date": "Tue, 07 Feb 2017 06:12:40 +0000", "extension": "docx", "file": "..\/uploads\/dK079QrL2g.d
{
"files": [
"..\/uploads\/dK079QrL2g.docx"
],
"metas": [
{
"date": "Tue, 07 Feb 2017 06:12:40 +0000",
"extension": "docx",
"file": "..\/uploads\/dK079QrL2g.docx",
"name": "dK079QrL2g.docx",
"old_name": "Cover letter - Copy.docx",
"replaced": false,
"size": 16720,
"size2": "16.33 KB",
"type": [
"application",
"vnd.openxmlformats-officedocument.wordprocessingml.document"
]
}
]
}
然而,在我实现之后,我在我的网站上得到的输出看起来不一样?这个和上面的有什么区别
Array
(
[files] => Array
(
[0] => uploads/iD2eBCwStA.docx
)
[metas] => Array
(
[0] => Array
(
[date] => Tue, 07 Feb 2017 07:57:03 +0100
[extension] => docx
[file] => uploads/iD2eBCwStA.docx
[name] => iD2eBCwStA.docx
[old_name] => Cover letter - Copy.docx
[replaced] =>
[size] => 16720
[size2] => 16.33 KB
[type] => Array
(
[0] => application
[1] => vnd.openxmlformats-officedocument.wordprocessingml.document
)
)
)
)
这是我的JS
//filter uploader
$('#files').filer({
showThumbs: true,
maxSize : 25,
fileMaxSize : 25,
addMore: true,
allowDuplicates: false,
extension : ["png", "gif"],
uploadFile: {
url: "ajax/upload.php",
data: {},
type: 'POST',
enctype: 'multipart/form-data',
beforeSend: function(){},
success: function(data, el){
var parent = el.find(".jFiler-jProgressBar").parent();
el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
$("<div class=\"jFiler-item-others text-success\"><i class=\"icon-jfi-check-circle\"></i> Success</div>").hide().appendTo(parent).fadeIn("slow");
});
console.log(data);
},
error: function(el){
var parent = el.find(".jFiler-jProgressBar").parent();
el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
$("<div class=\"jFiler-item-others text-error\"><i class=\"icon-jfi-minus-circle\"></i> Error</div>").hide().appendTo(parent).fadeIn("slow");
});
},
statusCode: null,
onProgress: null,
onComplete: null
}
});
//过滤器上载程序
$('#文件')。文件管理器({
秀拇指:没错,
最大尺寸:25,
fileMaxSize:25,
艾德莫尔:没错,
allowDuplicates:false,
扩展名:[“png”、“gif”],
上载文件:{
url:“ajax/upload.php”,
数据:{},
键入:“POST”,
enctype:“多部分/表单数据”,
beforeSend:function(){},
成功:功能(数据,el){
var parent=el.find(“.jFiler jProgressBar”).parent();
el.find(“.jFiler jProgressBar”).fadeOut(“慢”,函数(){
$(“Success”).hide().appendTo(parent.fadeIn(“slow”);
});
控制台日志(数据);
},
错误:函数(el){
var parent=el.find(“.jFiler jProgressBar”).parent();
el.find(“.jFiler jProgressBar”).fadeOut(“慢”,函数(){
$(“Error”).hide().appendTo(parent.fadeIn(“slow”);
});
},
状态代码:null,
onProgress:null,
onComplete:null
}
});
第一个结果是JSON
输出,另一个返回响应的数组
要获得JSON结果,需要在JS中指定
dataType:JSON
希望这有帮助,第一个结果是JSON
输出,另一个返回响应的数组
要获得JSON结果,需要在JS中指定
dataType:JSON
希望这对您有所帮助您看到的值是您从服务器获得的JSON,它已转换为JavaScript对象(然后转换为字符串以显示)。如果您想将其转换回JSON并显示,可以使用console.log(JSON.stringify(data))代码>
也就是说,您可能希望将数据作为JavaScript对象,因为它更易于使用。例如,您可以使用值data.metas.size
执行涉及上载文件大小的计算(请尝试:console.log(data.metas.size2+)(“+data.metas.size+”bytes);
)。JSON表示法无法做到这一点:它只是一个字符串。您看到的值是在将JSON转换为JavaScript对象(然后转换为字符串以显示)后从服务器获得的JSON。如果您想将其转换回JSON并显示,可以使用console.log(JSON.stringify(data))代码>
也就是说,您可能希望将数据作为JavaScript对象,因为它更易于使用。例如,您可以使用值data.metas.size
执行涉及上载文件大小的计算(请尝试:console.log(data.metas.size2+)(“+data.metas.size+”bytes);
)。JSON表示法无法做到这一点:它只是一个字符串