【问题标题】:Issues uploading files to server with phonegap cordova使用 phonegap cordova 将文件上传到服务器的问题
【发布时间】:2017-04-20 08:42:18
【问题描述】:

我有一个非常简单的应用程序,可以从相机和库中上传图片。我设法让它按预期工作,但由于某种我不知道的原因,它停止了工作。我什至从提交中下载了文件,我确定它可以正常工作,但它不再可用了。

该应用程序让您拍照,从图库中选择一张,然后添加一个小缩略图(这是有效的)拍摄/选择的照片,然后让您提交表单。上传会在您选择文件后立即进行,并且表单提交会发送一封电子邮件,其中包含这些文件作为附件(在服务器上完成)。

上传函数总是返回“发生错误:代码 = 1”

编辑:我正在通过将我的设备连接到 phonegap windows 服务器来测试应用程序。

EDIT2:显然问题出在 phonegap 仿真器上,尝试了真正的 apk 安装,并且成功了。

代码:

// Wait for device API libraries to load
  //
  document.addEventListener("deviceready", onDeviceReady, false);
  // device APIs are available
  //
  function onDeviceReady() {
      // Now safe to use device APIs
      //alert("Device Ready");
      /*button that opens camera and takes picture*/
      $("#add_photo").click(function(){
          navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
          sourceType: Camera.PictureSourceType.CAMERA,
          destinationType: Camera.DestinationType.FILE_URI });
      });

      /*on success show thumbnail and try to upload*/
      function onSuccess(imageURI) {
          $("#preview").append("<img src='"+imageURI+"'/>");
          fileUpload(imageURI, "image/jpeg");
      }

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

      function fileUpload(imageURI, mimeType){
        var win = function (r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
            $('#form_submit').prop('disabled', false);
            $('#form_submit').prop('value', 'ENVIAR');
            //alert("done");
            //alert(r.response);
        }
        var fail = function (error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
        options.mimeType = mimeType;
        var params = {};
        i++;
        sent_files[i] = session_id+"-"+i+"."+imageURI.split('.').pop();
        params.session_id = sent_files[i];
        $('[name=send]').val(sent_files.join());
        options.params = params;
        $('#form_submit').prop('disabled', true);
        $('#form_submit').prop('value', 'CARGANDO');
        var ft = new FileTransfer();
        ft.upload(imageURI, encodeURI("http://myServer/folder"), win, fail, options);
      };
  }

【问题讨论】:

  • 在真实设备安装上尝试过,而不是使用 phonegap 模拟器,它按预期工作。

标签: android ios cordova


【解决方案1】:

我在真实设备安装上尝试过,而不是使用 phonegap windows 服务器模拟器,它按预期工作。

我真傻。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多