【问题标题】:Cordova file transfer image is not uploading the file to serverCordova 文件传输图像未将文件上传到服务器
【发布时间】:2018-02-18 10:13:04
【问题描述】:

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

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

Cordova -> 7.1.0,Phonegap -> 7.1.1,cordova-plugin-file-transfer-> spec=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,
}

【问题讨论】:

  • 签入到 window.resolveLocalFileSystemURL() 和 toInternalURL 以获取源代码。我的应用程序这样调用(它在生产和工作中): window.resolveLocalFileSystemURL(imageUri, function (entry) { getB64Image(entry.toInternalURL()); }); //其中getB64Image是创建FileUploadOptions/FileTransfer/etc,并上传的函数。

标签: javascript android cordova image-uploading


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    fileUrl 必须是

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

    the docs

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

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多