Ionic2 在ionic 2中上载时重命名图像文件

Ionic2 在ionic 2中上载时重命名图像文件,ionic2,ionic3,Ionic2,Ionic3,我在网上偶然发现了这个功能 private createFileName() { var d = new Date(), n = d.getTime(), newFileName = n + ".jpg"; return newFileName; } 它的用途是重命名图像文件。我的问题是 这是否仅将图像上载限制为jpg,还是将其转换为 jpg 如果是前者,我如何在上传之前获得图像扩展 然后像这样修改private createFileN

我在网上偶然发现了这个功能

private createFileName() {
      var d = new Date(),
      n = d.getTime(),
      newFileName =  n + ".jpg";
      return newFileName;
    }
它的用途是重命名图像文件。我的问题是

  • 这是否仅将图像上载限制为
    jpg
    ,还是将其转换为
    jpg
  • 如果是前者,我如何在上传之前获得图像扩展 然后像这样修改
    private createFileName(ext)
    函数,以便上传 每种图像类型
  • 如果有帮助,这里是完整的代码

    lastImage: string = null;
      loading: Loading;
    
    public takePicture(sourceType) {
      // Create options for the Camera Dialog
      var options = {
        quality: 100,
        sourceType: sourceType,
        saveToPhotoAlbum: false,
        correctOrientation: true
      };
    
      // Get the data of an image
      this.camera.getPicture(options).then((imagePath) => {
        // Special handling for Android library
        if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
          this.filePath.resolveNativePath(imagePath)
            .then(filePath => {
              let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
              let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
              this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
            });
        } else {
          var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
          var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
          this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
        }
      }, (err) => {
        this.presentToast('Error while selecting image.');
      });
    }
    
       private copyFileToLocalDir(namePath, currentName, newFileName) {
      this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
        this.lastImage = newFileName;
      }, error => {
        this.presentToast('Error while storing file.');
      });
    }
    
    
    private presentToast(text) {
      let toast = this.toastCtrl.create({
        message: text,
        duration: 3000,
        position: 'top'
      });
      toast.present();
    }
    
    // Always get the accurate path to your apps folder
    public pathForImage(img) {
      if (img === null) {
        return '';
      } else {
        return cordova.file.dataDirectory + img;
      }
    }
    
    public uploadImage() {
      // Destination URL
      var url = "http://my_url/uploads.php";
    
      // File for Upload
      var targetPath = this.pathForImage(this.lastImage);
    
      // File name only
      var filename = this.lastImage;
    
      var options = {
        fileKey: "file",
        fileName: filename,
        chunkedMode: false,
        mimeType: "multipart/form-data",
        params : {'fileName': filename}
      };
    
      const fileTransfer: TransferObject = this.transfer.create();
    
      this.loading = this.loadingCtrl.create({
        content: 'Uploading...',
      });
      this.loading.present();
    
      // Use the FileTransfer to upload the image
      fileTransfer.upload(targetPath, url, options).then(data => {
        this.loading.dismissAll()
        this.presentToast('Image succesfully uploaded.');
      }, err => {
        this.loading.dismissAll()
        this.presentToast('Error while uploading file.');
      });
    }
    


    1-它不限于“jpg”,也不转换为“jpg”,正如方法名所说:创建文件名,它创建一个字符串,文件名就是这个字符串。

    2-图像扩展名将为jpg,因为它是默认的相机编码类型,并且看起来您正在使用相机拍照。您可以在照相机选项中更改编码类型。

    您可以在中找到所有相机选项