I have a problem selecting files and uploading to a server. For Android my code works perfectly using the file-transfer and file-selector plugins. However on iOS it does not work for selecting files. For images I got it.
the file picker/chooser works only in Android. Any ideia ?
My code:
$$('#arquivoUpload').on('click', function () {
fileChooser.open(function(file) {
$("#nome").html(file.name);
$("#mime").html(file.mime_type);
$("#extensao").html(file.extension);
$("#local").html(file.uri);
var caminhoLocal=file.uri;
var nomeArquivo=file.name;
var mimeTipo=file.mime_type;
app.dialog.preloader('Fazendo Upload...');
var certo = function (r) {
console.log("Code = " + r.responseCode);
console.log("Resposta = " + r.response);
console.log("Sent = " + r.bytesSent);
if (r.response==0){
app.dialog.close();
app.dialog.alert('Por favor, tente novamente.','<b>Upload não realizado</b>');
}
if (r.response!==0){
app.dialog.close();
$("#nomeUpload").html(r.response);
app.dialog.alert('Arquivo Salva no Servidor','<b>UPLOAD REALIZADO!</b>');
}
}
var falhou = function (error) {
app.dialog.close();
app.dialog.alert('Por favor, tente novamente.','<b>Upload não realizado</b>');
app.dialog.alert("Houve um problema. Código: " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = nomeArquivo;
options.mimeType = mimeTipo; )
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(caminhoLocal,"upload.php", certo, falhou, options)
});
This sounds like you would need to request permission from the user to access the iOS files app.
Hi Kerry,
Using the input type file html tag I can access the files, but I am not able to understand the reason for not uploading the .php file on my server. Anyway thanks for the help!
Mu code modified:
$$(’#abreFile’).on(‘change’, function (file) {
var caminhoLocal = "/var/mobile/Applications/<UUID>/</UUID>";
var tabelachat = localStorage.getItem("tabelachat");
app.dialog.preloader('Anexando arquivo...');
var certo = function (r) {
console.log("Resposta = " + r.response);
if (r.response==0){
app.dialog.close();
app.dialog.alert('Por favor, tente novamente.','<b>Upload não realizado</b>');
}
if (r.response!==0){
app.dialog.close();
$("#nomeUpload").html(r.response);
app.dialog.alert('Arquivo Salvo no Servidor','<b>Arquivo anexado!</b>');
}
}
var falhou = function (error) {
app.dialog.close();
app.dialog.alert('tente novamente.','<b>Upload não realizado</b>');
app.dialog.alert("Houve um problema. Código: " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = nomeArquivo;
options.mimeType = mimeTipo;
options.chunkedMode = false;
var params = {};
params.value1 = nome;
params.value2 = login;
params.value3 = tabelachat;
params.value4 = mimeTipo;
options.params = params;
var ft = new FileTransfer();
ft.upload(caminhoLocal, "upload-arquivo.php", certo, falhou, options)
});
Did you whitelist your server for iOS? It might be blocking the connection.
What I had to do was use the plugin FilePicker-Phonegap-iOS-Plugin for iOS and adapt my code.
SOLUTION :
$$(‘pick’).on(‘click’, function() {
FilePicker.pickFile(successCallback,errorCallback);
});
function successCallback(path) {
var caminhoLocal = path;
var tabelachat = localStorage.getItem("tabelachat");
app.dialog.preloader('Anexando arquivo...');
var certo = function (r) {
console.log("Resposta = " + r.response);
if (r.response==0){
app.dialog.close();
app.dialog.alert('Por favor, tente novamente.','<b>Upload não realizado</b>');
}
if (r.response!==0){
app.dialog.close();
$("#nomeUpload").html(r.response);
app.dialog.alert('Arquivo Salvo no Servidor','<b>Arquivo anexado!</b>');
}
}
var falhou = function (error) {
app.dialog.close();
app.dialog.alert('tente novamente.','<b>Upload não realizado</b>');
app.dialog.alert("Houve um problema. Código: " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = caminhoLocal.substr(caminhoLocal.lastIndexOf('/') + 1);
options.mimeType = "image/jpg"; //
options.chunkedMode = false;
var params = {};
params.value1 = nome;
params.value2 = login;
params.value3 = tabelachat;
options.params = params;
var ft = new FileTransfer();
ft.upload(caminhoLocal, "upload-arquivo.php", certo, falhou, options)
}
anyway thank you