2016-08-04 179 views
0

使用cordova File-Transfer插件我嘗試上傳圖片。 但上傳失敗。 我的代碼如下所示。cordova上的文件上傳失敗

function capturePhoto() { 
    navigator.camera.getPicture(
     uploadPhoto, 
     onFail, 
     {quality: 50, destinationType: Camera.DestinationType.FILE_URI} 
    ); 
} 

function uploadPhoto(imageURI) { 
    console.log(imageURI); 
    var largeImage = document.getElementById('largeImage'); 
    largeImage.style.display = 'block'; 
    largeImage.src = imageURI; 
    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    var userid = '123456'; 
    var imagefilename = userid + Number(new Date()) + ".jpg"; 
    options.fileName = imagefilename; 
    options.mimeType = "image/jpg"; 
    var params = new Object(); 
    params.imageURI = imageURI; 
    params.userid = '1';//sessionStorage.loginuserid; 
    options.params = params; 
    options.chunkedMode = false; 
    var ft = new FileTransfer(); 
    var url = "http://localhost:8080/video/upload.php"; 
    //console.log('sd'); 
    ft.upload(imageURI, url, win, fail, options, true); 
} 
function win(r) { 
    console.log("Image uploaded successfully!!"); 
} 
function fail(error) { 
    console.log(error); 
    console.log("There was an error uploading image"); 
} 
function onFail(message) { 
    console.log('Failed because: ' + message); 
} 

在此先感謝。

+0

什麼是在後端,?通過https://gonzalo123.com/2013/10/28/taking-photos-with-a-phonegapcordova-application-and-uploading-them-to-the-server/,它會幫助你 – Naitik

+0

後端是PHP。它在我的本地服務器上運行。 – Rahul

回答

0

OK,然後你的代碼應該像如下:

的JavaScript:

var pictureSource; // picture source 
var destinationType; // sets the format of returned value 

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    pictureSource = navigator.camera.PictureSourceType; 
    destinationType = navigator.camera.DestinationType; 
} 

function clearCache() { 
    navigator.camera.cleanup(); 
} 

var retries = 0; 
function onCapturePhoto(fileURI) { 
    var win = function (r) { 
     clearCache(); 
     retries = 0; 
     alert('Done!'); 
    } 

    var fail = function (error) { 
     if (retries == 0) { 
      retries ++ 
      setTimeout(function() { 
       onCapturePhoto(fileURI) 
      }, 1000) 
     } else { 
      retries = 0; 
      clearCache(); 
      alert('Ups. Something wrong happens!'); 
     } 
    } 

    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1); 
    options.mimeType = "image/jpeg"; 
    options.params = {}; // if we need to send parameters to the server request 
    var ft = new FileTransfer(); 
    ft.upload(fileURI, encodeURI("http://host/upload"), win, fail, options); 
} 

function capturePhoto() { 
    navigator.camera.getPicture(onCapturePhoto, onFail, { 
     quality: 100, 
     destinationType: destinationType.FILE_URI 
    }); 
} 

function onFail(message) { 
    alert('Failed because: ' + message); 
} 

PHP

服務器上使PHP文件邊我身邊我使用XAMPP PHP的,所以我把我的php文件放入c:/xampp/htdocs並從xampp cotrol pannel開始Apache,然後我把這條線作爲encodeURI("192.168.1.124/uploadfile.php")

uploadfile.php

<?php 
move_uploaded_file($_FILES["file"]["tmp_name"], '/path/to/file'); 
?> 

平安評論,如果您有任何困惑。

+0

使用此代碼「Ups。發生錯誤!」警報消息中顯示的消息。 – Rahul

+0

先嚐試使用小尺寸圖像 – Naitik

+0

我試過了12kb的圖像大小。但同樣的事情發生。 – Rahul