2017-02-22 54 views
0

我有PhoneGap奇怪的問題,我試圖從應用程序記錄視頻,並將其上傳到服務器。PhoneGap視頻記錄並上傳到服務器

app.initialize(); 

    $(function(){ 
     //capture callback 
     var captureSuccess = function(mediaFiles) { 
     var i, len; 
     for(i = 0, len = mediaFiles.length; i < len; i += 1) { 
      uploadFile(mediaFiles[i]); 
     } 
     }; 

     function uploadFile(mediaFiles) { 
     var options = new FileUploadOptions(); 
     options.fileKey = 'file'; 
     options.fileName = mediaFiles.name; 
     options.contentType = 'video/mp4'; 
     options.mimeType = mediaFiles.type; 
     options.chunkedMode = true; 

     var ft = new FileTransfer(); 
     var path = mediaFiles.fullPath; 
     //var name = mediaFiles.name; 

     ft.upload(path, 
      'http://domainName/~me/upload.php', 
      function(result) { //success callback 
      navigator.notification.alert('Path: ' + path + ' bytesSent: ' + result.bytesSent + ' responseCode: ' + result.responseCode + ' response: ' + result.response, null, 'Upload Error'); 
      $("#infoMessage").html('Success!'); 
      }, 
      function(error) { //error callback 
      navigator.notification.alert('Error uploading file ' + path + ', Error code: ' + error.code + ' Target: ' + error.target + ' http_status: ' + error.http_status + ' Body: ' + error.body + ' Exception: ' + error.exception.toString(), null, 'Upload Error'); 
      $("#infoMessage").html('Problem') 
      }, 

      options); 
     } 

     // capture error callback 
     var captureError = function(error) { 
     navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error'); 
     }; 

     //start video capture 
     var accessCamera = function() { 
     navigator.device.capture.captureVideo(captureSuccess, captureError, { limit:1 }); 
     }; 

       $("#cambtn").on('click', accessCamera); 

服務器端正在運行,因爲它應該用郵遞員應用程序進行測試。 迴應,我從PhoneGap的應用「成功回調」得到的是:Screenshot

服務器端腳本是如此基本的現在:

$target_dir = "uploads/"; 
if(isset($_FILES['file'])) { 
    $target_file = $target_dir . basename($_FILES["file"]["name"]); 
    move_uploaded_file($_FILES['file']['tmp_name'], $target_file); 
} 
print_r($_FILES); //This line returns empty array when request is send by the application but it returns array with data when I test it with Postman. 
die(); 

問題是由於chunkedMode選項。它必須設置爲false。

回答

0

你的成功回調函數說「上傳錯誤」,如果你一路滾動到右側

 function(result) { //success callback 
      navigator.notification.alert('Path: ' + path + ' bytesSent: ' + result.bytesSent + ' responseCode: ' + result.responseCode + ' response: ' + result.response, null, 'Upload Error'); 
      $("#infoMessage").html('Success!'); 
+0

這最後一個參數只是一個彈出通知的標題,你可以在截圖這麼看,這不是問題在所有。 – xcentric

+0

不好意思誤解了你的問題。在看到代碼中的註釋以解釋錯誤之前,我在圖片上看到了「上傳錯誤」。我會檢查android監視器和服務器日誌。你可以讓php寫一個日誌並在那裏排除故障。我正在看http://stackoverflow.com/questions/29099303/uploading-file-over-https-with-cordova-filetransfer並試圖查看是否有可能成功塊被調用,即使有錯誤如果該網址沒有被列入白名單或類似的東西, –

+0

也檢查標題('訪問控制,允許來源:*');在你的php –