如何移动 tmp uri 中包含 /private/ 的文件

How can I move a file that has /private/ in the tmp uri

我有 ionic 4 应用程序,可以拍照并保存到应用程序中,以便将来在手机连接更好时上传。我已经成功地处理了照片,现在正在尝试处理 iOS 设备上的文件。我正在使用 IOSFilePicker 插件获取 uri link 以复制 tmp 文件夹中的文件,然后将其复制到目录。这对文件失败。我认为这与 uri

中的 private 有关

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

成功保存照片的日志

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 当前名称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

失败记录

获取文件当前名称photo2.jpg

获取文件正确路径/private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-收件箱/

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-收件箱/

copyFileToLocalDir 当前名称photo2.jpg

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

错误{"code":5,"message":"ENCODING_ERR"}

我能看到的唯一真正区别是 namePath 中有 /private/。两个文件都是 .jpg 照片。如果这是问题所在,有没有办法绕过这个/private/。

我的代码如下

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.');
  });
} 

原来 /private/ 是问题所在。最后做的是切掉 /private/ 并在其位置添加 file:// 然后它工作正常

下面是我编辑的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));
}