2012-12-17 43 views
6

Tôi đang cố gắng tải tệp lên máy chủ của mình bằng Phonegap. Tôi hiện đang bị mắc kẹt khi một lỗi mà nói:Tải lên hình ảnh bằng Phonegap: 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 + Javascript

<!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> 

hoàn chỉnh bản ghi lỗi.

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). 

Có ai có ý tưởng về cách thực hiện tác phẩm này không?

Cảm ơn!

W

+0

Do bản cập nhật này, tôi không thể sửa lỗi này trên Trình mô phỏng Windows Phone. Tôi đã thử nghiệm điều này trên iPhone và nó hoạt động. – wenbert

Trả lời

1

Tôi nghĩ rằng bạn đang định dạng sai giá trị tùy chọn của mình. Bạn có cần phải vượt qua JSON hoặc một đối tượng thực tế không?

Ngay bây giờ bạn đang truyền một mảng có văn bản trong đó.

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}"] 

Lỗi dường như liên quan đến các vấn đề deserialization.

0

Tôi gặp sự cố tương tự như của bạn. Tôi đã giải quyết điều này, thay đổi tham số mimeType thành 'text/plain'.

Bạn có sử dụng thông số để gửi không? Nếu đó là sai tôi nghĩ rằng bạn cần gửi params trống.

+0

Tôi đã thử sử dụng 'text/plain' cho mimeType và nó không hoạt động. – wenbert

0

Tôi có vấn đề này trước khi, hãy thử để chuẩn bị hình ảnh trong html đầu tiên, và không mang nó trực tiếp từ hoa tiêu, nó có thể không lưu ảnh chụp vào đó tiền mặt;)

Trong giải pháp của tôi, tôi giả sử để có một Tage hình ảnh với id = 'camera_image'

img id = 'camera_image' ...

Sau đó, tôi đặt tất cả các biến của hình ảnh trong đó và tôi tải nó lên (như bạn sẽ thấy trong đoạn mã sau).

đây là 2 chức năng i sử dụng:

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; 
     } 
} 

cho việc lựa chọn một hình ảnh hiện tại:

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; 
     } 

    } 

trong chức năng tải lên: chức năng uploadPhoto (img) { imageURI = img.src. ..

ps: xin lỗi vì định dạng mã của tôi, nó không khắc phục tốt.

+0

@wenbert cảm ơn bạn đã sửa định dạng :) –

1

Đặt getImage của bạn, uploadImage, giành chiến thắng, thất bại bên ngoài $ (tài liệu) cuộc gọi hàm nội tuyến của .ready.

Tham chiếu để thắng và thất bại thực sự bị đóng và khoảng cách điện thoại nhận nó là vô giá trị khi cố gắng truy cập trực tiếp vào các phương thức đó. Phonegap có thể là một hàm toàn cục thay vì hàm ẩn bên trong một hàm.

PhoneGap thực thi ngữ cảnh javascript bên mã của nó, những gì có thể hoạt động trong thời trang javascript thực có thể không hoạt động chính xác với điện thoại.

+0

Không. Đặt phương thức getImage và các phương thức khác bên ngoài hàm '$ (document) .ready' của jQuery không hoạt động. Tôi gặp lỗi tương tự. – wenbert

Các vấn đề liên quan