首页 文章

使用phonegap cordova将文件上传到服务器的问题

提问于
浏览
0

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

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

上传功能始终返回“发生错误:代码= 1”

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

EDIT2: apparently the problem was the phonegap emulator, tried on a real apk installation and it worked.

码:

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

1 回答

  • 0

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

    傻我 .

相关问题