apache / cordova-plugin-file-transfer

Apache Cordova File Transfer Plugin
https://cordova.apache.org/
Apache License 2.0
598 stars 885 forks source link

cordova build iOS fails #297

Closed jfoclpf closed 3 years ago

jfoclpf commented 3 years ago

Bug Report

Problem

What is expected to happen?

the build of iOS via cordova build ios should work correctly

What does actually happen?

it throws an error

** BUILD FAILED **

The following build commands failed:
    CompileC /Users/user926670/Library/Developer/Xcode/DerivedData/Denúncia_Estacionamento-cakwmvmhffwxigdavpvkssbcuvdh/Build/Intermediates.noindex/Denúncia\ Estacionamento.build/Debug-iphonesimulator/Denúncia\ Estacionamento.build/Objects-normal/x86_64/CDVFileTransfer.o /Users/user926670/dev/form-for-parking-violation/platforms/ios/Denúncia\ 
Estacionamento/Plugins/cordova-plugin-file-transfer/CDVFileTransfer.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler

(1 failure)

xcodebuild: Command failed with exit code 65

when I remove this plugin the build succeeds

Information

$ cordova info

Cordova Packages:

    cli: 10.0.0
        common: 4.0.2
        create: 3.0.0
        lib: 10.0.0
            common: 4.0.2
            fetch: 3.0.0
            serve: 4.0.0

Project Installed Platforms:

    ios: 6.1.1

Project Installed Plugins:

    cordova-pdf-generator: 2.1.1
    cordova-plugin-app-version: 0.1.12
    cordova-plugin-camera: 5.0.1
    cordova-plugin-compat: 1.2.0
    cordova-plugin-device: 2.0.3
    cordova-plugin-email-composer: 0.9.2
    cordova-plugin-file-transfer: 1.7.1
    cordova-plugin-file: 6.0.2
    cordova-plugin-geolocation: 4.1.0
    cordova-plugin-inappbrowser: 4.1.0
    cordova-plugin-is-debug: 1.0.0
    cordova-plugin-mobile-ocr: 3.1.2
    cordova-plugin-network-information: 2.0.2
    cordova-plugin-screen-orientation: 3.0.2
    cordova-plugin-simple-image-resizer: 0.1.1
    cordova-plugin-splashscreen: 6.0.0
    cordova-plugin-statusbar: 2.4.3
    cordova-plugin-whitelist: 1.3.4
    info.protonet.imageresizer: 0.1.1

Environment:

    OS: macOS Catalina 10.15.7 (19H114) (darwin 19.6.0) x64
    Node: v12.16.2
    npm: 6.14.4

ios Environment:

    xcodebuild:
Xcode 12.3
Build version 12C33

Project Setting Files:

    config.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.form.parking.violation" version="2.6.4" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>Denúncia Estacionamento</name>
  <description>
        Envio de queixa de estacionamento ilegal a autoridade competente
    </description>
  <author email="joao.pimentel.ferreira@gmail.com" href="https://www.joaopimentel.com/">
        João Pimentel Ferreira
    </author>
  <content src="index.html"/>
  <icon height="512" src="res/icon/universal/icon4_512x512_playstore.png" width="512"/>
  <icon density="xhdpi" height="196" src="res/icon/universal/icon4_XHDPI_196x196_320dpi.png" width="196"/>
  <icon density="xxxhdpi" height="192" src="res/icon/universal/icon4_XXXHDPI_192x192_640dpi.png" width="192"/>
  <icon density="xxhdpi" height="144" src="res/icon/universal/icon4_XXHDPI_144x144_480dpi.png" width="144"/>
  <icon density="hdpi" height="72" src="res/icon/universal/icon4_HDPI_72x72_240dpi.png" width="72"/>
  <icon density="mdpi" height="48" src="res/icon/universal/icon4_MDPI_48x48_160dpi.png" width="48"/>
  <access origin="*"/>
  <allow-navigation href="*"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <preference name="windows-target-version" value="10.0"/>
  <preference name="windows-phone-target-version" value="10.0"/>
  <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle"/>
  <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets"/>
  <preference name="StatusBarOverlaysWebView" value="false"/>
  <preference name="StatusBarBackgroundColor" value="#FFFFFF"/>
  <preference name="StatusBarStyle" value="blacktranslucent"/>
  <hook src="hooks/importNpmPackages.js" type="before_prepare"/>
  <hook src="hooks/copyCredentials.js" type="before_prepare"/>
  <hook src="hooks/convertHbsToHtml.js" type="after_prepare"/>
  <hook src="hooks/minifyFiles.js" type="after_prepare"/>
  <platform name="android">
    <preference name="android-minSdkVersion" value="22"/>
    <preference name="android-targetSdkVersion" value="29"/>
    <allow-intent href="market:*"/>
    <icon height="512" src="res/icon/android/512.png" width="512"/>
    <icon density="xhdpi" height="192" src="res/icon/android/192.png" width="192"/>
    <icon density="xxxhdpi" height="192" src="res/icon/android/192.png" width="192"/>
    <icon density="xxhdpi" height="144" src="res/icon/android/144.png" width="144"/>
    <icon density="hdpi" height="72" src="res/icon/android/72.png" width="72"/>
    <icon density="mdpi" height="48" src="res/icon/android/48.png" width="48"/>
    <splash density="hdpi" src="res/screen/android/screen-hdpi-portrait.png"/>
    <splash density="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png"/>
    <splash density="ldpi" src="res/screen/android/screen-ldpi-portrait.png"/>
    <splash density="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png"/>
    <splash density="mdpi" src="res/screen/android/screen-mdpi-portrait.png"/>
    <splash density="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png"/>
    <splash density="xhdpi" src="res/screen/android/screen-xhdpi-portrait.png"/>
    <splash density="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
</widget>

    package.json:
--- Start of Cordova JSON Snippet ---
{
  "plugins": {
    "cordova-plugin-geolocation": {
      "GPS_REQUIRED": "true"
    },
    "cordova-plugin-email-composer": {},
    "info.protonet.imageresizer": {},
    "cordova-plugin-simple-image-resizer": {
      "ANDROID_EXIFINTERFACES_VERSION": "27.+"
    },
    "cordova-plugin-file-transfer": {},
    "cordova-plugin-file": {},
    "cordova-plugin-statusbar": {},
    "cordova-plugin-screen-orientation": {},
    "cordova-plugin-device": {},
    "cordova-plugin-whitelist": {},
    "cordova-plugin-network-information": {},
    "cordova-plugin-compat": {},
    "cordova-pdf-generator": {},
    "cordova-plugin-splashscreen": {},
    "cordova-plugin-inappbrowser": {},
    "cordova-plugin-is-debug": {},
    "cordova-plugin-app-version": {},
    "cordova-plugin-camera": {
      "ANDROID_SUPPORT_V4_VERSION": "27.+"
    },
    "cordova-plugin-mobile-ocr": {}
  },
  "platforms": [
    "browser",
    "android",
    "ios"
  ]
}
--- End of Cordova JSON Snippet ---

Command or Code

cordova build ios

Environment, Platform, Device

iOS

Version information

macOS, standard cordova

Checklist

timbru31 commented 3 years ago

Which version are you installing? The current git master or the latest npm release? The latter does not work with cordova-ios@6 yet.

erisu commented 3 years ago

@timbru31 It looks like they are using the npm released version.

The supplied printout of the cordova info command show cordova-plugin-file-transfer: 1.7.1.

If they were using the git's main branch, it should show 2.0.0-dev I blieve.

I guess they can try the git repo version, since the npm release does not work with cordova-ios@6.x, as you pointed out.

timbru31 commented 3 years ago

I scrolled too fast through the list and missed the plugins section.

Yes, the master works and we don't have an ETA for npm releases.

jfoclpf commented 3 years ago

Sorry, I got lost. How do I install the plug-in then without error on build?

On Mon, 4 Jan 2021, 11:27 Tim Brust, notifications@github.com wrote:

Closed #297 https://github.com/apache/cordova-plugin-file-transfer/issues/297.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/apache/cordova-plugin-file-transfer/issues/297#event-4164329793, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6M4DPXM7IAXDH25FNNBRDSYGJZXANCNFSM4VSXE3AQ .

timbru31 commented 3 years ago

cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master

jfoclpf commented 3 years ago

Thanks a lot. Just one more further tiny question :)

I read your blog post wherein you refer this plugin is due to be deprecated (you use the word "sunsetting") because now we can do the same with pure javascript.

I just use this plugin to upload a file. Can you tell the corresponding javascript code for the method ft.upload?

Thanks a lot in advance

On Mon, 4 Jan 2021, 13:32 Tim Brust, notifications@github.com wrote:

cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/apache/cordova-plugin-file-transfer/issues/297#issuecomment-753949419, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6M4DJJI32N3LQ7TWCPPTLSYGYMTANCNFSM4VSXE3AQ .

breautek commented 3 years ago

I read your blog post wherein you refer this plugin is due to be deprecated (you use the word "sunsetting") because now we can do the same with pure javascript.

This plugin was deprecated (and there hasn't been a release to "undeprecate" it yet), but it was voted to bring it back because pure JS solutions still has some flaws in regards to efficiently working with large files.

Can you tell the corresponding javascript code for the method ft.upload?

You can read the file as a blob using the cordova-plugin-file plugin. Example from the docs:

function readBinaryFile(fileEntry) {

    fileEntry.file(function (file) {
        var reader = new FileReader();

        reader.onloadend = function() {
            var blob = new Blob([new Uint8Array(this.result)], { type: "application/octet-stream" });
        };

        reader.readAsArrayBuffer(file);

    }, onErrorReadFile);
}

Once you have your blob object, you can upload using XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'example.com/upload');
xhr.setRequestRequestHeader('Content-Type', theFileMimeType);
xhr.onload = function() {
   // finished uploading (or error)... check xhr.status to be sure.
};
xhr.send(blob);

xhr.send accepts strings as well, so if you're files are textual (like JSON, csvs, etc...), you can also read the files as a string and upload. But the example above should work with any data type, including binary data like images.

jfoclpf commented 3 years ago

Thanks, I'm here for around 2 days to try to figure out exactly on how to upload a file in cordova using pure javascript. Please DO NOT deprecate this plugin because that's exactly the purposes of plugins and npm packages, to ease other people's work avoiding extra burden with something that someone had already developed. One single line which would suffice to upload a simple file with the help of this plugin, is becoming extremely cumbersome.

That's what i got by now, but still with no success. I can't figure out the exact function which emulates ft.upload (which was working)

function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
      fileEntry.file(function (file) {
        var reader = new FileReader()
        reader.onloadend = function () {
          console.log('this.result', this.result)
          var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
          console.log('blob', blob)
          var fd = new FormData()
          fd.append('blob', blob, fileName)

          var xhr = new XMLHttpRequest()
          xhr.open('POST', remoteUrl)
          xhr.setRequestHeader('Content-Type', 'multipart/form-data')
          xhr.onload = function () {
            if (xhr.status === 200) {
              console.success(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
              if (typeof callback === 'function') { callback() }
            } else {
              console.error(`Error uploading file ${fileUri} to server`)
              console.error(xhr.status)
              if (typeof callback === 'function') { callback(xhr.status) }
            }
          }
          xhr.onerror = function (err) {
            console.error(`Error uploading file ${fileUri} to server`)
            console.error(err)
            if (typeof callback === 'function') { callback(err) }
          }
          xhr.send(fd)
        }
        console.log('file', file)
        reader.readAsArrayBuffer(file)
      }, function (err) {
        console.error(`Error uploading file ${fileUri} to server`)
        console.error(err)
        if (typeof callback === 'function') { callback(err) }
      })
    })
  }

On the server side I use this: https://attacomsian.com/blog/uploading-files-nodejs-express

I don't want steal your time, but can you see what am I missing?

jfoclpf commented 3 years ago

just to share my results, here a working pure JS function equivalent to ft.upload

  function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
      fileEntry.file(function (file) {
        var reader = new FileReader()
        reader.onloadend = function () {
          var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
          var fd = new FormData()

          fd.append('file', blob, fileName)

          var xhr = new XMLHttpRequest()
          xhr.open('POST', remoteUrl, true)
          xhr.onload = function () {
            if (xhr.status === 200) {
              console.log(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
              if (typeof callback === 'function') { callback() }
            } else {
              console.error(`Error uploading file ${fileUri}. Server returned ${xhr.status}`)
              if (typeof callback === 'function') { callback(xhr.status) }
            }
          }
          xhr.onerror = function (err) {
            console.error(`Error uploading file ${fileUri} to server`)
            console.error(err)
            if (typeof callback === 'function') { callback(err) }
          }
          xhr.send(fd)
        }
        reader.readAsArrayBuffer(file)
      }, function (err) {
        console.error(`Error uploading file ${fileUri} to server`)
        console.error(err)
        if (typeof callback === 'function') { callback(err) }
      })
    })
  }
jfoclpf commented 3 years ago

I made a gist on the subject if you're interested https://gist.github.com/jfoclpf/07e52f6bdf9c967449c4bc06af44c94a

Thanks

klochko7 commented 3 years ago

Good day! I have the same problem. Help please. What can I do?

Use ios: 6.1.1 cordova-plugin-file-transfer: 2.0.0-dev

Build failed.

The following build commands failed: CompileC /var/root/Library/Developer/Xcode/DerivedData/MyApp-dmqwhbrrkdtjbwagcrsqusbgfgsz/Build/Intermediates.noindex/MyApp.build/Debug-iphonesimulator/MyApp.build/ Objects-normal/x86_64/CDVFileTransfer.o /Users/ionicdev/Public/Ionic\ 5/Ionic/platforms/ios/MyApp/Plugins/cordova-plugin-file-transfer/CDVFileTransfer.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler (1 failure) xcodebuild: Command failed with exit code 65

jfoclpf commented 3 years ago

Hi @klochko7 use the dev version of the plugin instead: cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master If you want to avoid the plugin at all and just use pure html5 functions, check my gist: https://gist.github.com/jfoclpf/07e52f6bdf9c967449c4bc06af44c94a

klochko7 commented 3 years ago

Hi, jfoclpf. I am using 2.0.0-dev. It dose not build. Am I right ?

jfoclpf commented 3 years ago

according to @timbru31 it should work. I just use now pure javascript. Just copy the functions and it works.

faizan28 commented 3 years ago

Hi @klochko7 use the dev version of the plugin instead: cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master If you want to avoid the plugin at all and just use pure html5 functions, check my gist: https://gist.github.com/jfoclpf/07e52f6bdf9c967449c4bc06af44c94a

Hi @jfoclpf thank you for this HOT fix. I was facing the same issue but after removing my current plugin then install cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master version fixed my issue on IOS.

jfoclpf commented 3 years ago

BTW, @klochko7 did you remove the old plugin version?

cordova plugin rm cordova-plugin-file-transfer
cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master
klochko7 commented 3 years ago

BTW, @klochko7 did you remove the old plugin version?

cordova plugin rm cordova-plugin-file-transfer
cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#master

I will try. Thank you very much.