Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ionic 3使用表单数据发布图像_Php_Angular_Ionic3 - Fatal编程技术网

Php Ionic 3使用表单数据发布图像

Php Ionic 3使用表单数据发布图像,php,angular,ionic3,Php,Angular,Ionic3,我正在尝试将表单数据,包括文本和图像文件发布到PHP服务器。 我使用ionic native相机从gallery拍摄照片: const options: CameraOptions = { quality: 70, destinationType: this.camera.DestinationType.FILE_URI, sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, saveToPhotoAlbu

我正在尝试将表单数据,包括文本和图像文件发布到PHP服务器。 我使用ionic native相机从gallery拍摄照片:

const options: CameraOptions = {
    quality: 70,
    destinationType: this.camera.DestinationType.FILE_URI,
    sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
    saveToPhotoAlbum:false,
    targetWidth: 400,
    targetHeight: 400
}

this.camera.getPicture(options).then((imageData) => {
  this.myphoto = normalizeURL(imageData);
  this.myphoto2 = imageData;
}, (err) => {

});
并使用表单数据发布:

var headers = new Headers();
headers.append('Content-Type', 'multipart/form-data;boundary=' + Math.random());
headers.append('Accept', 'application/json');
let options = new RequestOptions({
    headers: headers
});
let formData = new FormData();
formData.append('judul', this.judul.value);
formData.append('photo', this.myphoto, '123.jpg');
this.http.post('http://localhost/upload.php', formData, options)
    .map(...)
    .subscribe(...);
但是,我在PHP日志中看到,表单数据不是由爱奥尼亚发送的。 这里怎么了

  • 您可以尝试从this.http.post()参数中删除“选项”。 您的代码变成:

    this.http.post(“”,formData) .map(…) .认购(……)

  • 如果这不起作用, 尝试使用以下命令跨整个BASE64映像发送

    'destinationType:this.camera.destinationType.DATA\u URL'


  • 我建议您使用本机插件上载文件:


    首先从相机上拍照

    async takePhoto() {
    const options: CameraOptions = {
        quality: 70,
        destinationType: this.camera.DestinationType.FILE_URI,
        sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
        saveToPhotoAlbum:false,
        targetWidth: 400,
        targetHeight: 400
    }
    
    this.camera.getPicture(options).then((imageData) => {
      this.getSystemURL(imageData);
    }, (err) => {
    
    });
    }
    
    然后获取本地系统映像URL

    private getSystemURL(imageFileUri: any): void {
        this.file.resolveLocalFilesystemUrl(imageFileUri)
            .then(entry => (entry as FileEntry).file(file => {
              this.readFile(file);
            }))
            .catch(err => console.log(err));
      }
    
    private readFile(file: any) {
        const reader = new FileReader();
        reader.onloadend = () => {
          this.myphoto = new Blob([reader.result], {type: file.type});
          var headers = new Headers();
    headers.append('Content-Type', 'multipart/form-data;boundary=' + Math.random());
    headers.append('Accept', 'application/json');
    let options = new RequestOptions({
        headers: headers
    });
    let formData = new FormData();
    formData.append('judul', this.judul.value);
    formData.append('photo', this.myphoto, '123.jpg');
    this.http.post('http://localhost/upload.php', formData, options)
        .map(...)
        .subscribe(...);
        };
        reader.readAsArrayBuffer(file);
      }
    
    在读了那个图片URL之后

    private getSystemURL(imageFileUri: any): void {
        this.file.resolveLocalFilesystemUrl(imageFileUri)
            .then(entry => (entry as FileEntry).file(file => {
              this.readFile(file);
            }))
            .catch(err => console.log(err));
      }
    
    private readFile(file: any) {
        const reader = new FileReader();
        reader.onloadend = () => {
          this.myphoto = new Blob([reader.result], {type: file.type});
          var headers = new Headers();
    headers.append('Content-Type', 'multipart/form-data;boundary=' + Math.random());
    headers.append('Accept', 'application/json');
    let options = new RequestOptions({
        headers: headers
    });
    let formData = new FormData();
    formData.append('judul', this.judul.value);
    formData.append('photo', this.myphoto, '123.jpg');
    this.http.post('http://localhost/upload.php', formData, options)
        .map(...)
        .subscribe(...);
        };
        reader.readAsArrayBuffer(file);
      }
    
    它为我工作,
    希望能有所帮助。

    我尝试了选项2,但似乎base64字符串太长,因此引发了一个错误。您遇到了什么错误,base64字符串永远不会太长,这取决于文件大小?文件有多重?后处理从未完成,无限挂起并且不会返回任何内容。选项一对您有效吗?如果没有错误信息,我真的无法帮助你。是的,有时它能工作,但有时不行。错误消息只是连接超时。