jQuery AJAX 上传图片 => PHP 发回带有 HTML 而不是 JSON 的响应文本



我的目标是通过jQuery AJAX将图像上传到PHP SYMFONY。多亏了我在其他SO线程上得到的输入,我成功地将文件转到了PHP端。现在,我期望从PHP返回AJAX的答案是JSON。

我可以看到我的PHP创建了JSON并将其发送回,但由于某些原因(我怀疑这是PHP.INI设置),AJAX对象在其responseText中的开头有一些HTML:

"<br /> <b>Notice</b>:  Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br /> "{u0022statusu0022:u0022successu0022,u0022fileUploadedu0022:true}""

我在AJAX端的代码看起来是这样的:

var pictureFormData = new FormData();
pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);
$.ajax({
url: [my php to integrate image].php,
data: pictureFormData,
type:"post",
contentType:false,
processData:false,
cache:false,
dataType:"json",
success:function(res){
console.log('AJAX SUCCESS');
console.info(res);
},
error:function(err){
console.log('AJAX ERR');
console.info(err);
},
timeout:function(){
console.log('AJAX TIMEOUT');
},
complete: function(){
console.log('AJAX COMPLETE');
}
});

在我的PHP SYMFONY端,处理AJAX调用的CONTROLLER如下所示:

class AjaxImageController extends Controller
{
public function indexAction(Request $request)
{
$file = $request->files->get('pictureFile');
$status = array('status' => "success","fileUploaded" => false);
// If a file was uploaded
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$path = "/tmp";
$file->move($path,$filename); // move the file to a path
$status = array('status' => "success","fileUploaded" => true);
}
$data = json_encode($status);

$response = new JsonResponse();
$response->setData($data);

dump($response);
return $response;
}
}

正如你所看到的,我在寄回之前做了一个dump(response)。其内容如下:

JsonResponse {#719 ▼
#data: ""{u0022statusu0022:u0022successu0022,u0022fileUploadedu0022:true}""
#callback: null
#encodingOptions: 15
+headers: ResponseHeaderBag {#722 ▼
#computedCacheControl: array:2 [▼
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:2 [▼
"cache-control" => "Cache-Control"
"content-type" => "Content-Type"
]
#headers: array:2 [▼
"cache-control" => array:1 [▼
0 => "no-cache, private"
]
"content-type" => array:1 [▼
0 => "application/json"
]
]
#cacheControl: []
}
#content: ""{u0022statusu0022:u0022successu0022,u0022fileUploadedu0022:true}""
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}

因此,我不明白为什么responseText一开始就错误地包含HTML:"<br /> <b>Notice</b>: Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br />",然后触发AJAX调用的error函数。

一些更新:我注意到以下内容:

在客户端,当我将pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);嵌入到我发送给SYMFONY PHP服务器的内容中时:我在CONTROLLER传入的$request中注意到该文件出现在FileBag对象中:

Request {#9 ▼
+attributes: ParameterBag {#12 ▶}
+request: ParameterBag {#10 ▶}
+query: ParameterBag {#11 ▶}
+server: ServerBag {#16 ▶}
+files: FileBag {#14 ▼
#parameters: array:1 [▼
"pictureFile" => UploadedFile {#15 ▼
.... all files data
}
]
}
...
}

然后我得到我描述的错误

如果我拿走从客户端发送的文件,并检查CONTROLLER,$request看起来像这样:

Request {#9 ▼
...      +files: FileBag {#14 ▼
#parameters: []
}
...
}

在这种情况下,错误不会出现。我怀疑因此对FileBag对象进行了操作,以某种方式抛出echo

您的响应包含HTML,因为php抛出了一个错误。如果你纠正了它,那么反应就会很好。

文件似乎不存在。

如果您在服务器上或XHR请求中没有看到该文件,则需要在表单上添加enctype="multipart/form-data"

如果表单和XHR请求是可以的,那么尝试通过将根目录添加到上传目录来上传文件。

# app/config/config.yml
# ...
parameters:
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'

$file->move(
$this->getParameter('brochures_directory'),
$fileName
);

或者只是在你的控制器中

$root = $this->get('kernel')->getRootDir();
$path = $root."/tmp";
$file->move($path,$filename); // move the file to a path

问题是由PHP.INI设置引起的:

文件夹:upload_tmp_dir=[path to php]\php7\uploadtemp在php.INI.中设置

"[path to php]\php7\"存在,但没有文件夹"uploadtemp",它无法创建它。

我在"[path to php]\php7\"中创建了文件夹"uploadtemp",它解决了这个问题

最新更新