cordova-plugin-file未上传cdvfile://服务器路径

e5njpo68  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(134)

可以肯定的是,将文件从本地路径传输到远程服务器不再需要cordova-plugin-file-transfer
我能够得到cdvfile://路径,甚至在DOM上显示相同的路径,但我不知道如何使用文件插件而不是文件传输来上传它。
下面是我目前使用的插件代码:

  • cordova -插件-相机
  • cordova插件文件
  • cordova 插穗作物

编码:

function clickFirstProfilePhoto(){

    navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
    correctOrientation:true, 
    targetWidth:1024, 
    targetHeight: 1024, 
    destinationType: 
    destinationType.FILE_URI});
}

现在成功函数:

function onPhotoDataSuccess(imageData) {
           
plugins.crop.promise(imageData).then(function success (imageFinal) {
          // Success.
          //alert(imageFinal);
          var fileURI = imageFinal.substr(imageFinal.lastIndexOf('?') + 1);
          //alert(fileURI);
          resolveLocalFileSystemURL(imageFinal, function(entry) {
          $("#picPreviewBox").html('<img src="'+entry.toInternalURL()+'" width="100%" />');

            var t=""
            t=t+'<div class="btn btn-dark btn-sm">Save</div> <div class="btn btn-dark btn-sm">discard</div>';
            $("#buttons_save_discard").html(t);

            
          });

}).catch(function fail (err) {
          // fail
 $.alert("Seems your phone resources are too low to proceed further");
        });

 }

在DOM中使用entry.toInternalURL()是显示捕获的图片,然后裁剪,但如何上传到服务器上的url沿着一些参数?

ddrv8njm

ddrv8njm1#

好了,现在我有了解决方案。在获得文件路径后,我将其转换为base64,然后将其上传到服务器上的PHP文件中,将其保存为图像,下面是代码:

function onPhotoDataSuccess(imageData) {
               
    plugins.crop.promise(imageData).then(function success (imageFinal) {
              // Success.
              //alert(imageFinal);
              var fileURI = imageFinal.substr(imageFinal.lastIndexOf('?') + 1);
              //alert(fileURI);
              resolveLocalFileSystemURL(imageFinal, function(entry) {
                
                $("#loadSignup3Data").html('<img src="'+entry.toInternalURL()+'" id="imagePreviewBox" width="200p" height="200" style="border-radius:50%;" />');
               // $("#picPreview").modal('show');
                $("#picOptions").modal('hide');
                $("#picPreviewBox").html('<img src="'+entry.toInternalURL()+'" id="imagePreviewBox" width="200p" height="200"/>');
                //$("#picPreview").modal('show');
               $.confirm({
                title: 'Please confirm!',
                content: 'Do you wish to save it as your profile picture?',
                buttons: {
                    confirm: function () {
                        
                        $("#ultraLoader").show();
                        html2canvas(document.querySelector("#imagePreviewBox")).then(canvas => {
                            var renderedImg = canvas.toDataURL();
                            var id = $("#pictureUploadID").val();
                            var deviceid = device.uuid;
                          
          $.ajax({
          type: "POST",
          url: "https://my_url_which_i_must_not_show/saveFirstProfilePic.php",
          dataType:'json',
          data: {base64Img: renderedImg, id:id, deviceid:deviceid},
          success: function(response){
             $("#ultraLoader").hide();
             $.alert("Your profile picture has been saved");
             location.href = "Dashboard.html?id="+id;
             }
            });

          });
             
   },cancel: function () {
    navigator.notification.alert('You chose not to save the picture. Please take another picture or select one from your phone gallery');
       $("#ultraLoader").hide();
                 }
                }
            });

相关问题