简体   繁体   English

ionic 4 (android) 从图库 (FILE_URI) 获取图像 (OBJECT FILE) 并通过 API 上传

[英]ionic 4 (android) get image (OBJECT FILE) from gallery (FILE_URI) and upload via API

I'm trying to implement a simple application using Ionic v4 angular and cordova.我正在尝试使用Ionic v4 angular 和cordova实现一个简单的应用程序 Just select a photo and upload it to a parse server (back4app.com) .只需选择一张照片并将其上传到解析服务器 (back4app.com) 即可 But I couldn't do it.但我做不到。

This is my code:这是我的代码:

home.page.ts主页.ts

import { ParseService } from '../service/parse.service';

import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { File } from '@ionic-native/file/ngx';
import { WebView } from '@ionic-native/ionic-webview/ngx';
  selectPhoto() {
    const options: CameraOptions = {
      quality: 100,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true 
    }
    this.camera.getPicture(options).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64 (DATA_URL):
      // let base64Image = 'data:image/jpeg;base64,' + imageData;
      // console.log(imageData);

      this.fileToUpload = imageData;

      this.phototoshow = this.webview.convertFileSrc(imageData);

    }, (err) => {
      // Handle error
    });
  }
 onSubmit() {

    this.presentLoading();

    // return this.apiService.upload(this.phototoshow).subscribe((data: any) => {
    return this.apiService.upload(this.fileToUpload).subscribe((data: any) => {
          console.log(data);
    }
}

service.ts服务.ts

upload(img1): Observable<any> {
    // console.log(data);
    return this.http.post(this.apiURL + '/files/img.jpg', img1,{
      headers: {
        'X-Parse-Application-Id': this.APP_ID,
        'X-Parse-REST-API-Key': this.REST_API_KEY,
        'Content-Type':'image/jpeg'
      }
    })
    .pipe(
      retry(1),
      catchError(this.handleError)
    )
  }

I was able to upload the image with "input type = file" in the form ... but selecting the image from the gallery with the cordova plugin camera ... it only returns FILE_URI but I need the OBJECT FILE to upload via api rest.我能够以表单中的“输入类型=文件”上传图像......但是使用cordova插件相机从图库中选择图像......它只返回FILE_URI但我需要OBJECT FILE通过api rest上传.

I have read enough info on the web but it is old information that does not help me.我在网上阅读了足够多的信息,但这些旧信息对我没有帮助。 I hope someone can help me with the problem.我希望有人能帮我解决这个问题。 thanks谢谢

I managed to solve the problem:我设法解决了这个问题:

  startUpload() {
      this.file.resolveLocalFilesystemUrl(this.fileToUpload)
      // this.file.resolveLocalFilesystemUrl(imgEntry.filePath)
          .then(entry => {
              (entry as FileEntry).file(file => this.readFile(file))
          })
          .catch(err => {
              alert('Error while reading file.');
          });
  }

  readFile(file: any) {
      const reader = new FileReader();
      reader.onload = () => {
          const imgBlob = new Blob([reader.result], {
              type: file.type
          });
          this.onSubmit(imgBlob);
      };
      reader.readAsArrayBuffer(file);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cordova navigator.camera.getPicture在Android 4.4 KitKat上以意外格式返回FILE_URI - Cordova navigator.camera.getPicture returns FILE_URI in unexpected format on Android 4.4 KitKat from gallery 如何使用Google Drive API上传FILE_URI:插入文件 - How to upload FILE_URI using Google Drive API: Insert File 离子-将图像源设置为FILE_URI会产生:不允许加载本地资源错误 - Ionic - Setting an image source as FILE_URI produces: Not allowed to load local resource error 从 android 上传图像文件到 laravel api - Upload image file to laravel api from android 从uri文件获取真实路径并将其上传到android - Get real path from uri file and upload it android 从图库上传图像到FTP没有这样的文件或目录 - Upload Image from Gallery to FTP No such file or directory IONIC 相机插件 FILE_URI 根据所选文件类型(视频/图片)返回不同的文件路径格式 - IONIC camera plugin FILE_URI returning different file path format based on selected file type (video/picture) 科尔多瓦相机插件Android将额外的信息附加到FILE_URI的末尾 - Cordova Camera Plugin Android appends extra information to the end of FILE_URI Cordova插件照片库插件:获取下载的图片file_uri路径 - Cordova Plugins Photo Library Plugin: Get downloaded picture file_uri path Android通过php上载图片库之前调整图片大小 - Android resize image from gallery before upload via php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM