2012-12-17 10 views
6

मैं अपने सर्वर पर फोनगैप के साथ एक फ़ाइल अपलोड करने का प्रयास कर रहा हूं। मैं वर्तमान में अटक कर रहा हूँ जब एक त्रुटि का कहना है कि:फोनगैप के साथ एक छवि अपलोड करें: InvalidCastException

InvalidCastException 
Failed to deserialize WP7CordovaClassLib.Cordova.Commands.FileTransfer+UploadOptions[] with JSON value :: ["{\"filePath\":\"/CapturedImagesCache/PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"server\":\"http://server.myapp.srv.co.nz/pages/fileupload\",\"fileKey\":\"file\",\"fileName\":\"PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"mimeType\":\"image/jpg\",\"params\":\"value1=test&value2=param\",\"chunkedMode\":false}"] 

HTML + जावास्क्रिप्ट

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <title>File Transfer Example</title> 

</head> 
<body> 
    <button id="uploadPhotoButton">Upload a Photo</button> 

    <script type="text/javascript" src="cordova-2.2.0.js"></script> 
    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> 
    <script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script> 
    <script type="text/javascript" src="js/camera.js"></script> 
    <script type="text/javascript"> 

    $(document).one("pause", function() { 
     console.log('Paused.'); 
    }); 

    $(document).one("resume", function() { 
     console.log('Resumed.'); 
    }); 

    $(document).one("deviceready", function() { 
     console.log('Device is ready.'); 
    }); 

    $(document).one("backbutton", function() { 
     console.log('Back button pressed.'); 
    }); 

    $(document).ready(function() { 
     console.log('DOM is ready.'); 

     $(document).on("click", "#uploadPhotoButton", function (e) { 
      console.log('clicked button'); 
      getImage(); 
     }); 


     function getImage() { 
      // Retrieve image file location from specified source 
       navigator.camera.getPicture(uploadPhoto, function (message) { 
        alert('get picture failed'); 
       }, { 
        quality: 50, 
        destinationType: navigator.camera.DestinationType.FILE_URI, 
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
       } 
      ); 

     } 

     function uploadPhoto(imageURI) { 
      var options = new FileUploadOptions(); 
      options.fileKey = "file"; 
      options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); 
      options.mimeType = "image/jpeg"; 

      var params = new Object(); 
      params.value1 = "test"; 
      params.value2 = "param"; 

      options.params = params; 
      options.chunkedMode = false; 

      var ft = new FileTransfer(); 
      ft.upload(imageURI, "http://my.server.co.nz/pages/fileupload", win, fail, options); 
     } 

     function win(r) { 
      console.log("Code = " + r.responseCode); 
      console.log("Response = " + r.response); 
      console.log("Sent = " + r.bytesSent); 
      alert(r.response); 
     } 

     function fail(error) { 
      alert("An error has occurred: Code = " = error.code); 
     } 
    }); 

    </script> 
    </body> 
</html> 

पूरा त्रुटि लॉग।

GapBrowser_Navigated :: /app/www/index.html#/app/www/uploadtest.html 
Log:"clicked button" 
The thread '<No Name>' (0xf55026a) has exited with code 0 (0x0). 
The thread '<No Name>' (0xe3f0326) has exited with code 0 (0x0). 
INFO: AppDeactivated 
INFO: AppActivated 
Log:"Paused." 
The thread '<No Name>' (0xf1a02e6) has exited with code 0 (0x0). 
Log:"Resumed." 
The thread '<No Name>' (0xf2a01d2) has exited with code 0 (0x0). 
options = ["{\"filePath\":\"/CapturedImagesCache/PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"server\":\"http://my.server.co.nz/pages/fileupload\",\"fileKey\":\"file\",\"fileName\":\"PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"mimeType\":\"image/jpg\",\"params\":\"value1=test&value2=param\",\"chunkedMode\":false}"] 
A first chance exception of type 'System.InvalidCastException' occurred in System.ServiceModel.Web.dll 
A first chance exception of type 'System.InvalidCastException' occurred in System.ServiceModel.Web.dll 
InvalidCastException 
Failed to deserialize WP7CordovaClassLib.Cordova.Commands.FileTransfer+UploadOptions[] with JSON value :: ["{\"filePath\":\"/CapturedImagesCache/PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"server\":\"http://server.myapp.srv.co.nz/pages/fileupload\",\"fileKey\":\"file\",\"fileName\":\"PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"mimeType\":\"image/jpg\",\"params\":\"value1=test&value2=param\",\"chunkedMode\":false}"] 
A first chance exception of type 'System.NullReferenceException' occurred in Lion.MyApp.dll 
The thread '<No Name>' (0xfdc025e) has exited with code 0 (0x0). 
Log:"Error in error callback: FileTransfer1325332352 = ReferenceError: Invalid left-hand side in assignment" 
The thread '<No Name>' (0xfa60286) has exited with code 0 (0x0). 

क्या किसी को यह काम करने के बारे में कोई विचार है?

धन्यवाद!

डब्ल्यू

+0

इस अद्यतन के रूप में, मैं इसे विंडोज फोन एमुलेटर पर ठीक करने में सक्षम नहीं हूं। मैंने आईफोन पर इसका परीक्षण किया और यह काम किया। – wenbert

उत्तर

1

मैं सोच रहा हूँ कि आप अपने विकल्पों मूल्य malforming कर रहे हैं। क्या आपको JSON या वास्तविक ऑब्जेक्ट को पास करने की आवश्यकता है?

अभी आप इसमें टेक्स्ट के साथ एक सरणी पारित कर रहे हैं।

options = ["{\"filePath\":\"/CapturedImagesCache/PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"server\":\"http://my.server.co.nz/pages/fileupload\",\"fileKey\":\"file\",\"fileName\":\"PhotoChooser-51766419-c657-46db-a53d-f09bee300a89.jpg\",\"mimeType\":\"image/jpg\",\"params\":\"value1=test&value2=param\",\"chunkedMode\":false}"] 

त्रुटि में विलुप्त होने के मुद्दे शामिल हैं।

0

मुझे आपकी तरह की समस्या थी। मैंने इसे हल किया, माइम टाइप पैरामीटर को 'टेक्स्ट/सादा' में बदल दिया।

क्या आप भेजने के लिए पैरा का उपयोग करते हैं? यदि यह गलत है तो मुझे लगता है कि आपको खाली पैरा भेजना होगा।

+0

मैंने माइम टाइप के लिए 'text/plain' का उपयोग करने का प्रयास किया और यह काम नहीं किया। प्रारूप को ठीक करने के लिए – wenbert

0

मैं था इस समस्या से पहले, पहले html में छवि तैयार करने के लिए प्रयास करें, और नाविक से सीधे इसे ले नहीं है, यह नकदी उस में लिया तस्वीर की बचत नहीं कर सकते हैं;)

मेरी समाधान में मुझे लगता है id = 'camera_image'

img id = 'camera_image' वाली किसी छवि tage के लिए ...

तो मैं इसे में छवि के सभी चर सेट और मैं इसे (अपलोड आप के रूप में निम्नलिखित कोड में देखेंगे)।

यहाँ 2 कार्यों मैं प्रयोग किया जाता है:

function takephoto(){  

     navigator.camera.getPicture(  
     function(uri){ 
      $('#camera_image').show(); 
      var img = document.getElementById('camera_image'); 
      img.style.visibility = "visible"; 
      img.style.display = "block"; 
      img.src = uri; 
      uploadPhoto(img);      
      alert("Success"); 
     }, 
     function(e) { 
      console.log("Error getting picture: " + e); 
     }, 
     { 
      quality: 50, 
      destinationType: navigator.camera.DestinationType.FILE_URI 
     }); 

     // Get URI of picture to upload 
     var img = document.getElementById('camera_image'); 
     var imageURI = img.src; 
     if (!imageURI || (img.style.display == "none")) { 
       alert("Take picture or select picture from library first."); 
       return; 
     } 
} 

एक मौजूदा तस्वीर को चुनने के लिए:

function choosephoto(){ 
    navigator.camera.getPicture(
     function(uri) { 
      $('#camera_image').show(); 
      var img = document.getElementById('camera_image'); 
      img.style.visibility = "visible"; 
      img.style.display = "block"; 
      img.src = uri; 
      uploadPhoto(img); 
     }, 
     function(e) { 
      console.log("Error getting picture: " + e); 
     }, 
     { 
      quality: 50, 
      destinationType: navigator.camera.DestinationType.FILE_URI, 
      sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM 
     });    

     // Get URI of picture to upload 
     var img = document.getElementById('camera_image'); 
     var imageURI = img.src; 
     if (!imageURI || (img.style.display == "none")) { 
      alert("please select a pic first"); 
      return; 
     } 

    } 
अपलोड समारोह में

: समारोह uploadPhoto (img) { imageURI = img.src। ..

ps: मेरे कोड के स्वरूपण के लिए खेद है, यह ठीक से ठीक नहीं है।

+0

@ वेनबर्ट धन्यवाद :) –

1

अपनी getImage, अपलोड इमेज, जीत, $ (दस्तावेज़) के बाहर असफल रहें। पहले से ही इनलाइन फ़ंक्शन कॉल।

जीतने और असफल होने का संदर्भ वास्तव में बंद हो जाता है, और जब फोन उन तरीकों तक सीधे पहुंचने का प्रयास कर रहा है तो फ़ोन अंतराल शून्य हो जाता है। फोनगैप शायद किसी फ़ंक्शन के अंदर छिपे हुए फ़ंक्शन के बजाय वैश्विक फ़ंक्शन की अपेक्षा कर रहा है।

फ़ोनगैप अपने कोड को साइड जावास्क्रिप्ट संदर्भ निष्पादित करता है, वास्तविक जावास्क्रिप्ट फैशन में क्या काम कर सकता है फोनगैप के साथ सही ढंग से काम नहीं कर सकता है।

+0

नहीं। JQuery के '$ (दस्तावेज़) के बाहर getImage और अन्य विधियों को रखना .ready' function काम नहीं किया। मुझे भी यही त्रुटि मिली। – wenbert