【问题标题】:Ionic image upload with PHP server使用 PHP 服务器上传 Ionic 图像
【发布时间】:2018-08-05 23:44:28
【问题描述】:

我的 ionic 应用程序有问题。 当我单击一个按钮时,我想将图像上传到我的 php 服务器,但似乎我做错了什么......

Communication.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic3 Server Send Test
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-list>
  <ion-item>
<button ion-button (click)="uploadFile()">Upload</button>
</ion-item>
  </ion-list>

</ion-content>

Communication.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';

@Component({
    selector: 'communication',
    templateUrl: 'communication.html'
})


export class CommunicationPage {
  imageURI:any;
imageFileName:any;

constructor(public navCtrl: NavController,
  private transfer: FileTransfer) {}

  uploadFile() {

  const fileTransfer: FileTransferObject = this.transfer.create();

  let options: FileUploadOptions = {
    fileKey: 'ionicfile',
    fileName: 'ionicfile',
    chunkedMode: false,
    headers: {}
  }

  fileTransfer.upload('C:/Users/Nathan/Desktop/Recognize/src/pages/communication/test.png', 'http://someserver', options)
    .then((data) => {
    console.log(data+" Uploaded Successfully");
  }, (err) => {
    console.log(err);
  });
}
}

点击上传按钮时出现此错误:

FileTransferError {code: 1, source:"C:/Users/Nathan/Desktop/Recognize/src/pages/communication/test.png", target: "http://someserver", http_status: null, body: null, …}

我知道“test.png”文件的 url 有问题,因为代码 1 错误。

你有什么想法吗?

【问题讨论】:

    标签: php cordova ionic-framework ionic2 image-uploading


    【解决方案1】:

    你需要像这样在fileTransfer.upload()添加图片targetPath,

    var targetPath = this.file.dataDirectory + imgName;
    
    fileTransfer.upload(targetPath, 'http://someserver', options)
        .then((data) => {
        console.log(data+" Uploaded Successfully");
      }, (err) => {
        console.log(err);
    });
    

    【讨论】:

    • var targetPath = this.file.dataDirectory + 'test.png';我现在有这个错误:“CommunicationPage”类型上不存在属性“文件”。
    • 你需要添加离子原生文件插件来获取设备的默认路径。
    • 我做了并导入: import { File } from '@ionic-native/file';但仍然是同样的错误......
    • 像这样导入文件插件 import { File } from '@ionic-native/file';并像这样放入构造函数(私有文件:文件){},并在 app.module 文件中导入文件插件
    • ok 错误消失了,但现在点击按钮时出现错误:FileTransferError {code: 1, source: "filesystem:file:///persistent/test.png", target: "http: // someserver", http_status: null, body: null, …}
    【解决方案2】:

    Hey Nathan,您遇到的问题是因为文件的 URL,在这里您只提供了 test.png 的 URL。

    而不是你应该为cordova使用FileChooser插件,它给出了文件的绝对URL。

    import { FileChooser } from '@ionic-native/file-chooser';
    
    constructor(private fileChooser: FileChooser) { }
    function() {
    const fileTransfer: FileTransferObject = this.transfer.create();
    
      let options: FileUploadOptions = {
        fileKey: 'ionicfile',
        fileName: 'ionicfile',
        chunkedMode: false,
        headers: {}
      }
    
    
    this.fileChooser.open()
      .then(uri => {
          fileTransfer.upload(uri, 'http://someserver', options)
        .then((data) => {
        console.log(data+" Uploaded Successfully");
      }, (err) => {
        console.log(err);
      });
      })
      .catch(e => console.log(e));
    
    }
    

    如果您需要更多帮助,请发表评论。

    【讨论】:

    • 我不想手动选择文件。当我点击按钮时,我只想将我的图片“test.png”上传到服务器上。
    • 你能告诉我图像在你的项目中的位置吗,你需要把图像的绝对路径例如 => D:/folder/project_folder/src/assets/imgs/test.png
    • 我的图片在通讯文件夹中。有同样的错误,即使我把绝对路径: C:/Users/Nathan/Desktop/Recognize/src/pages/communication/test.png
    • 将该图像放在 imgs 子文件夹的 assets 文件夹中并使用该路径,您将得到结果。 @na
    • 同样的错误... FileTransferError {code: 1, source: "C:/Users/Nathan/Desktop/Recognize/src/assets/imgs/test.png", target: "http:// / someserver", http_status: null, body: null, ...}
    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 2018-06-11
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多