2012-04-25 12 views
8

के साथ ट्रांसफर करें मैं FileTransfer method का उपयोग कर फ़ोनगैप से एक फ़ाइल को अपलोड करने का प्रयास कर रहा हूं। मुझे इस अपलोड के लिए सक्षम होने के लिए HTTP मूल लेख की आवश्यकता है।फोनगैप फ़ाइल HTTP बुनियादी प्रमाणीकरण

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 ऐसा लगता है कि मैं के रूप में मैं ऊपर किया है "पैरामीटर" सूची में "हेडर" शामिल करके प्राधिकरण हैडर निर्दिष्ट कर सकते हैं:

यहाँ प्रासंगिक कोड है

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

हालांकि, यह वास्तव में हेडर जोड़ने के लिए प्रतीत नहीं होता है।

तो: क्या आईफोन और एंड्रॉइड दोनों के लिए फोनगैप के फाइल ट्रान्सफर के साथ HTTP मूल लेख करने का कोई तरीका है?

+0

सोच किसी के लिए, इस विधि से ऊपर सूचीबद्ध मेरे लिए काम करता है। बस इसे जोड़ने की जरूरत है: 'params.headers = {प्रमाणीकरण:' बेसिक '+ क्रेडिट}; options.params = params; ' – gabaum10

+0

शीर्षकों को विकल्पों में जाना होगा। विकल्प विकल्पों में नहीं जोड़े गए हैं। पैरामीटर .____ – Adam

उत्तर

9

तुम इतनी तरह उन्हें विकल्पों में शामिल करके कस्टम शीर्षलेख जोड़ सकते बल्कि पैरामीटर से:

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

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

एफवाईआई, मुझे आईओएस (1/14/13 के रूप में) में इसके साथ समस्याएं हैं। एंड्रॉइड और बीबी में ठीक काम करता है ... – gabaum10

+0

आप किस फोनगैप का उपयोग कर रहे हैं? मुझे 2.3.0 – Ryan

+0

2.2.0 के साथ आईओएस पर सफलता मिली है ... यह दिलचस्प है, शायद मुझे अपग्रेड करने का प्रयास करना चाहिए? क्या आपको 2.2.0 के साथ पहले समस्याएं थीं? – gabaum10

0

हेडर सरणी के लिए सही स्थान विकल्पों में से एक तत्काल बच्चे के रूप में है। विकल्प> हेडर। विकल्प नहीं-> पैरा-> हेडर। https://github.com/apache/cordova-plugin-file-transfer

0

आप एक प्राधिकरण हैडर खुद बना सकते हैं:

//************************************************************** 
//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. 
}); 

यहाँ आधिकारिक दस्तावेज है: यहाँ एक उदाहरण है।

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

FileTransfer.js देखें कार्यान्वयन (लाइन 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; 
} 
संबंधित मुद्दे