CakePHP:发布的文件数据未包含在request-> data中 [英] CakePHP: posted file data not included in request->data
问题描述
我尝试将文件上传到第三方端点,但是我无法直接从我的表单发布文件,因为API需要一个api_key,我无法向最终用户公开。因此,我的计划是将表单指向一个控制器/操作,并从那里发布数据。但是,当我在控制器内部 debug($ this-> request-> data)
时,文件数据丢失。
I'm trying to upload a file to 3rd party endpoint, but I can't post the file directly from my form because the API requires an api_key which I can't expose to the end user. Therefore, my plan was to point the form to a controller/action and post the data from there. However, when I debug($this->request->data)
from inside the controller, the file data is missing.
视图上的表单:
echo $this->Form->create('Media', array('type'=>"file", 'url'=>array('controller'=>'media', 'action'=>'upload') ) );
echo $this->Form->input('name', array("name"=>"name") );
echo $this->Form->input('file', array('type'=>'file', "name"=>"file") );
echo $this->Form->input('project_id', array('type'=>'hidden', "name"=>"project_id", "value"=>$project["Project"]['hashed_id']) );
//THIS CANNOT BE HERE: echo $this->Form->input('api_password', array('type'=>'hidden', "name"=>"api_password", "value"=>'xxxxxxx') );
echo $this->Form->end("Submit");
这是我看到当我 debug()
来自控制器的请求数据:
Here's what what I see when I debug()
the request data from the controller:
array(
'name' => 'Some Name',
'project_id' => 'dylh360omu',
)
在这里?
推荐答案
文件上传数据只能在 CakeRequest :: data
如果输入元素名称在名为 data
的数组中传递(这是当不手动定义特定名称时的默认值),即:
File upload data can only be found in CakeRequest::data
in case the input element name is passed in an array named data
(which is the default when not defining a specific name manually), ie:
<input type='file' name='data[file]'>
然而,在您的情况下,元素将如下所示:
In your case however, the element will look like this:
<input type='file' name='file'>
这将导致文件数据放在 CakeRequest :: params [ form]
。
which will cause the files data to be put in CakeRequest::params[form]
.
https://github.com/cakephp/cakephp/blob/2.4.0/lib/Cake/Network/CakeRequest.php#L346 / a>
https://github.com/cakephp/cakephp/blob/2.4.0/lib/Cake/Network/CakeRequest.php#L346
因此,请更改表单中的名称:
So either change the name in the form accordingly:
$this->Form->input('file', array('type' => 'file', 'name' => 'data[file]'));
或者通过 CakeRequest :: params [form] code>:
Or access the file data via CakeRequest::params[form]
:
debug($this->request->params['form']);
这篇关于CakePHP:发布的文件数据未包含在request-> data中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!