2012-04-25 1 views
8

sto cercando di caricare un file da PhoneGap a un server utilizzando il FileTransfer method. Ho bisogno dell'autenticazione di base HTTP per essere abilitato per questo caricamento.PhoneGap FileTransfer con autenticazione HTTP di base

Ecco il codice rilevante:

var options = new FileUploadOptions({ 
     fileKey: "file", 
     params: { 
      id: my_id, 
      headers: { 'Authorization': _make_authstr() } 
     } 
    }); 
    var ft = new FileTransfer(); 
    ft.upload(image, 'http://locahost:8000/api/upload', success, error, options); 

Looking over the PhoneGap source code sembra che posso specificare l'intestazione di autorizzazione includendo "headers" nella lista "params" come ho fatto sopra:

 JSONObject headers = params.getJSONObject("headers"); 
     for (Iterator iter = headers.keys(); iter.hasNext();) 
     { 
     String headerKey = iter.next().toString(); 
     conn.setRequestProperty(headerKey, headers.getString(headerKey)); 
     } 

Tuttavia, questo non sembra aggiungere effettivamente l'intestazione.

Quindi: esiste un modo per eseguire l'autenticazione di base HTTP con FileTransfer di PhoneGap, sia per iPhone che per Android?

+0

Per chiunque chiedendo, questo metodo di cui sopra funziona per me. Basta aggiungere questo: 'params.headers = {Autorizzazione: 'Base' + creds}; options.params = params; ' – gabaum10

+0

intestazioni devono andare a options.headers non aggiunto a options.params .____ – Adam

risposta

9

È possibile aggiungere intestazioni personalizzate aggiungendoli ai opzioni, piuttosto che le params in questo modo:

authHeaderValue = function(username, password) { 
    var tok = username + ':' + password; 
    var hash = btoa(tok); 
    return "Basic " + hash; 
}; 

options.headers = {'Authorization': authHeaderValue('Bob', '1234') }; 
+0

FYI, Sto avendo problemi con questo in iOS (a partire dal 1/14/13). Funziona bene in Android e BB ... – gabaum10

+0

quale versione di PhoneGap stai usando? Ho avuto successo su iOS con 2.3.0 – Ryan

+0

2.2.0 ... è interessante, forse dovrei provare ad aggiornare? Avevi problemi prima con 2.2.0? – gabaum10

0

La posizione corretta per l'array intestazioni è come un figlio diretto di opzioni. Opzioni-> intestazioni. Non opzioni-> parametri-> intestazioni. Ecco un esempio:

//************************************************************** 
//Variables used below: 
//1 - image_name: contains the actual name of the image file. 
//2 - token: contains authorization token. In my case, JWT. 
//3 - UPLOAD_URL: URL to which the file will be uploaded. 
//4 - image_full_path - Full path for the picture to be uploaded. 
//*************************************************************** 
var options = { 
    fileKey: "file", 
    fileName: 'picture', 
    chunkedMode: false, 
    mimeType: "multipart/form-data", 
    params : {'fileName': image_name} 
}; 

var headers = {'Authorization':token}; 

//Here is the magic! 
options.headers = headers; 
//NOTE: I creaed a separate object for headers to better exemplify what 
// is going on here. Obviously you can simply add the header entry 
// directly to options object above. 

$cordovaFileTransfer.upload(UPLOAD_URL, image_full_path, options).then(
    function(result) { 
     //do whatever with the result here. 
}); 

Ecco la documentazione ufficiale: https://github.com/apache/cordova-plugin-file-transfer

0

È possibile creare un colpo di testa di autorizzazione da soli. Ma si può anche immettere le credenziali nell'URL come questo:

var username = "test", password = "pass";  
var uri = encodeURI("http://"+username + ':' + password +"@localhost:8000/api/upload"); 

See FileTransfer.js per l'attuazione (linea 45):

function getBasicAuthHeader(urlString) { 
var header = null; 


// This is changed due to MS Windows doesn't support credentials in http uris 
// so we detect them by regexp and strip off from result url 
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem 

if (window.btoa) { 
    var credentials = getUrlCredentials(urlString); 
    if (credentials) { 
     var authHeader = "Authorization"; 
     var authHeaderValue = "Basic " + window.btoa(credentials); 

     header = { 
      name : authHeader, 
      value : authHeaderValue 
     }; 
    } 
} 

return header; 
}