0
votes

I have and ionic 4 App that can take pictures and save to the App for future uploading when cell connection is better. I have been successful with photos and am now trying this with files that are on a iOS device. I am using the IOSFilePicker plugin to get a uri link to copy of the file in a tmp folder and then copy it to a Directory. This fails with files. I think it has something to do with private in the uri

copyFileToLocalDir namePath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox/

logs from successful photo save

createFileName newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpg

copyFileToLocalDir namePath file:///var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/

copyFileToLocalDir currentName cdv_photo_001.jpg

copyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpg copyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpg

Log from failure

getFile currentName photo2.jpg

getFile correctPath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox/

createFileNameAlt newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602196747

copyFileToLocalDir namePath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox/

copyFileToLocalDir currentName photo2.jpg

copyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602196747

err {"code":5,"message":"ENCODING_ERR"}

The only real difference I can see is the namePath has /private/ in it. both files are .jpg photos. Is there a way to bypass this /private/ if that is the issue.

My code is as follows

ts.

getFile() {
  this.filePicker.pickFile()
  .then((uri) => {
    console.log('uri', uri);
    var currentName = uri.substr(uri.lastIndexOf('/') + 1);
    var correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
    console.log("getFile currentName ", currentName);
    console.log("getFile correctPath ", correctPath);
    this.copyFileToLocalDir(correctPath, currentName, this.createFileNameAlt());
  }
  )
  .catch(err => console.log('Error', err));
}

createFileName() {
  var d = new Date(),
  n = d.getTime(),
  f = this.formID,
  q = this.sectionUUID,
  newFileName = "UUID" + "_" + f + "_" + "questionid_" + q + "_" + "time_" + n + ".jpg";
  console.log("createFileName newFileName ", newFileName);
  return newFileName;
}

createFileNameAlt() {
  var d = new Date(),
  n = d.getTime(),
  f = this.formID,
  q = this.sectionUUID,
  newFileName = "UUID" + "_" + f + "_" + "questionid_" + q + "_" + "time_" + n;
  console.log("createFileNameAlt  newFileName", newFileName);
  return newFileName;
}

copyFileToLocalDir(namePath, currentName, newFileName) {
  console.log("copyFileToLocalDir namePath", namePath);
  console.log("copyFileToLocalDir currentName", currentName);
  console.log("copyFileToLocalDir newFileName", newFileName);

  this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
    this.updateStoredImages(newFileName);
    console.log("copyFileToLocalDir newFileName ", newFileName);
  }, error => {
    let err = JSON.stringify(error);
    console.log("err", err);
    this.presentToast('Error while storing file.');
  });
} 
1

1 Answers

0
votes

Turns out the /private/ is the issue. What ended up doing was slice off the /private/ and add file:// in its place and then it worked fine

Below is my edited getFile()

getFile() {
  this.filePicker.pickFile()
  .then((uri) => {
    console.log('uri', uri);
    var currentName = uri.substr(uri.lastIndexOf('/') + 1);
    var correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
    let newName = correctPath.slice(8);
    let AmendLink = "file://" + newName;
    this.copyFileToLocalDir(AmendLink, currentName, this.createFileNameAlt());
  }
  )
  .catch(err => console.log('Error', err));
}