Cordova文件传输图像未将文件上载到服务器

时间:2018-02-09 13:29:41

标签: javascript android cordova image-uploading

请帮我解决这个问题,我试着从上周开始解决这个问题。使用cordova文件传输插件尝试将我的图像上传到生产服务器 但是我得到错误代码1.

我检查了源和目标路径都可以访问。请找到我的cordova安装版本。

Cordova - > 7.1.0, Phonegap - > 7.1.1, cordova-plugin-file-transfer - >规格= 1.7.1

代码:



function staticpathu_upload() {
   var fileURL = 'https://example.com/Al_2_1518090802.jpg';
   var uri = encodeURI('https://example.com/dummy.php');
 
   var options = new FileUploadOptions();
   options.fileKey = "file";
   options.fileName = fileURL.substr(fileURL.lastIndexOf('/')+1);
   options.mimeType = "image/jpeg";

   var ft = new FileTransfer();   		
   ft.upload(fileURL, uri, onSuccess, onError, options);

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

   function onError(error) {
	  console.log(error); 
      alert("An error has occurred: Code = " + error.code);
      console.log("upload error source " + error.source);
      console.log("upload error target " + error.target);
   }
	
}




错误:

FileTransferError {
   code: 1, 
   source: "https://example.com/Al_2_1518090802.jpg", 
   target: "https://example.com/dummy.php", 
   http_status: null, 
   body: null,
}

2 个答案:

答案 0 :(得分:0)

fileUrl必须是

  

表示设备上的文件或数据URI的文件系统URL。为了向后兼容,这也可以是设备上文件的完整路径。 (参见下面的向后兼容性说明)

请参阅the docs

这可能是抛出FileTransferError.FILE_NOT_FOUND_ERR(错误代码1)的原因。

答案 1 :(得分:0)

ServerUrl的这个问题,它不允许我将图像发布到服务器。我在服务器根文件夹上创建了dummy.php,并从下面的代码执行它工作正常。它不适用于我们仅在移动设备或模拟器中检查的桌面。

function uploadphoto(){ 
    navigator.camera.getPicture(uponSuccess, uponFail, { quality: 50,
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY, 
      allowEdit: true,
      destinationType: Camera.DestinationType.FILE_URI
    });

            // Change image source and upload photo to server
    function uponSuccess(imageURI) {

        // Set image source
        var image = document.getElementById('fileuploadimg');
        $$('.gAlbumList').append('<a href="#"><img src="'+imageURI  + '?' + Math.random()+'" /></a>');


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

        var params = {};
        params.value1 = "test";
        params.value2 = "param";

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

        var ft = new FileTransfer();

        ft.upload(imageURI, "https://example.com/dummy.php", function(result){
            $$('.error').append('<p>successfully uploaded ' + result.response+'</p>');
        }, function(error){
            $$('.error').append('<p>error : ' + JSON.stringify(error)+'</p>');
        }, options);

    }

    function uponFail(message) {
        $$('.error').append('<p>Failed because: ' + message+'</p>');
    }