使用Ionic

时间:2018-05-10 08:53:03

标签: javascript ionic-framework

我期待使用IONIC 3 / Cordova在Windows的本地目录中保存/上传图片。实际上,用户必须从文件夹中选择一个文件(jpg),我想将此文件复制到另一个目录中。

我尝试使用this.file.copyFile(path, name, newPath, newName),但它不起作用,我不明白为什么。我也尝试过使用文件传输插件(https://ionicframework.com/docs/native/file-transfer/),但似乎我必须在API中获取一个端点才能上传文件。

请在下面找到用户点击提交按钮时用来复制文件的功能:

private copyFileToLocalDirBrowser(namePath, currentName, newFileName) {
    this.file.copyFile(namePath, currentName, "file:///C://Users//myName//Desktop//imgs//", newFileName).then(success => {
      console.log("Picture imported!");
    }, error => {
      this.presentToast("Error:" + error);
    });
}

在构造函数中,我声明了private file: File,导入为import { File } from '@ionic-native/file';

用户点击输入节点(类型文件),选择他/她想要的文件:

<input name="pictureID" id="inputFile" type="file" (change)="showThumb($event)"/>

这里是showThumb函数:

    var files = e.target.files;
    for (var i = 0, f; f = files[i]; i++) {
      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          //Getting URI :
          if (e.target.result) {
            this.imageURI = e.target.result;
            // Render thumbnail.
            var img = document.createElement("img");
            img.setAttribute('src', this.imageURI);
            img.setAttribute('title', theFile.name);
            img.setAttribute('id', "thumb");
            document.getElementById('thumb-for-browser').appendChild(img);
          }
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }

然后,当用户点击&#34;提交&#34;按钮,我想保存我在本地目录中显示的图片&#34; file:/// C:// Users // myName // Desktop // imgs //&#34;。

提前感谢您的帮助。

亲切的问候,

0 个答案:

没有答案
相关问题