2
I’m trying to upload an image using $cordovaFileTransfer
. When I send the image I want to send tbm some parameters like the user id, the problem is that these parameters arrive null in the webservice.
I made my webservice in Cakephp and testing by Postman of Chrome I can normally send the parameters by POST, ie the webservice is working.
How can I send parameters using $cordovaFileTransfer ?
Controller
/** add image profile */
function addImagemUsuario(){
var img = document.getElementById('smallimage');
var _pathFile = img.src;
var _fileName = _pathFile.substr(_pathFile.lastIndexOf('/') + 1);
var _url = AppConstants.webServiceUrl + "/users/addImageUsuarioApp.json";
var _headers2 = HeaderProvider2.getHeader();
//options
var options = new FileUploadOptions();
options.chunkedMode = true;
options.mimeType = 'image/jpg';
options.fileKey = 'imageFile';
options.headers = _headers2;
options.fileName = _fileName;
//parametros
options.params = {
"idUsuario":"333",
};
$cordovaFileTransfer.upload(_url, _pathFile, options, true).then(function (result) {
console.log("Result: " + result);
//console.log("SUCCESS: " + JSON.stringify(result.response));
}, function (err) {
console.log("Erro: " + err);
//console.log("ERROR: " + JSON.stringify(err));
}, function (progress) {
// PROGRESS HANDLING GOES HERE
});
};
header
app.provider('HeaderProvider2', function(){
var email = '[email protected]';
var senha = 'password'
this.$get = function(Base64){
return {
getHeader: function(){
return {
'Authorization': 'Basic ' + Base64.encode(email + ':' + senha),
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}
}
};
};
});
Cakephp webservice
public function addImageUsuarioApp(){
$this->autoRender = false;
$id = $this->request->data["idUsuario"];
$imageFile = $_FILES["imageFile"];
$retorno = array("id"=>$id, "image"=>$imageFile);
return json_encode($retorno);
}
Retorno Webservice
Result: <pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa4db2-trace').style.display = (document.getElementById('cakeErr5654611fa4db2-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined index: idUsuario [<b>APP\Controller\UsersController.php</b>, line <b>200</b>]<div id="cakeErr5654611fa4db2-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa4db2-code').style.display = (document.getElementById('cakeErr5654611fa4db2-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa4db2-context').style.display = (document.getElementById('cakeErr5654611fa4db2-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5654611fa4db2-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">public function </span><span style="color: #0000BB">addImageUsuarioApp</span><span style="color: #007700">(){
</span></span></code>
<code><span style="color: #000000"><span style="color: #0000BB"> $this</span><span style="color: #007700">-></span><span style="color: #0000BB">autoRender </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;
</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> $id </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">request</span><span style="color: #007700">-></span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #DD0000">"idUsuario"</span><span style="color: #007700">];
</span></span></code></span></pre><pre class="stack-trace">UsersController::addImageUsuarioApp() - APP\Controller\UsersController.php, line 200
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 490
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
[main] - APP\webroot\index.php, line 115</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa6523-trace').style.display = (document.getElementById('cakeErr5654611fa6523-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined index: imageFile [<b>APP\Controller\UsersController.php</b>, line <b>201</b>]<div id="cakeErr5654611fa6523-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa6523-code').style.display = (document.getElementById('cakeErr5654611fa6523-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5654611fa6523-context').style.display = (document.getElementById('cakeErr5654611fa6523-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5654611fa6523-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $this</span><span style="color: #007700">-></span><span style="color: #0000BB">autoRender </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;
</span></span></code>
<code><span style="color: #000000"><span style="color: #0000BB"> $id </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">request</span><span style="color: #007700">-></span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #DD0000">"idUsuario"</span><span style="color: #007700">];
</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> $imageFile </span><span style="color: #007700">= </span><span style="color: #0000BB">$_FILES</span><span style="color: #007700">[</span><span style="color: #DD0000">"imageFile"</span><span style="color: #007700">];
</span></span></code></span></pre><pre id="cakeErr5654611fa6523-context" class="cake-context" style="display: none;">$id = null</pre><pre class="stack-trace">UsersController::addImageUsuarioApp() - APP\Controller\UsersController.php, line 201
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 490
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
[main] - APP\webroot\index.php, line 115</pre></div></pre>{"id":null,"image":null}
I believe the problem is time you recover these parameters in Cake, because on the side of the app seems to be right.
– Vinicius Dutra