【问题标题】:select image from computer in ionic app to serve it to server从 ionic 应用程序中的计算机中选择图像以将其提供给服务器
【发布时间】:2020-09-23 02:37:55
【问题描述】:

我的 ionic 应用程序有时会在浏览器中使用,我需要上传一些图片。 我已经实现了一个代码来选择我的文件,然后我有一个 formData 类型,我将图像放入其中。 我正在尝试使用发布请求将我的文件上传到服务器,但我不知道该怎么做。

当我在我的 php 代码中收到我的文件时,我将它插入到我的数据库中,我的数据库中有一个 blob 文件,但我认为它不是图像,它太小了......

html 文件:

  <input
  style="display: none"
  type="file" (change)="onFileChanged($event)"
  #fileInput>
<button (click)="fileInput.click()">Select File</button>
<button (click)="onUpload()">Upload!</button>

ts 文件:

onFileChanged(event) {
    this.selectedFile = <File>event.target.files[0];
    console.log(this.selectedFile);
  }

  onUpload() {
    // upload code goes here
      // this.http is the injected HttpClient
  let uploadData = new FormData();
  uploadData.append('file', this.selectedFile, this.selectedFile.name);
  console.log(this.selectedFile);
   this.http.post(this.server + 'saveExo.php', this.selectedFile)
     .subscribe(resData => {
         console.log(resData);
     });
  }

php 文件:

<?php


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Content-Type: application/json; charset=UTF-8');

include "db.php";

$postjson = json_decode(file_get_contents('php://input'), true);
$today = date('Y-m-d');

if ( isset( $_POST) ) {
    $result = json_encode(array('receive' => 'ok'));
    $query = mysqli_query($mysqli, "INSERT INTO EXO SET 
    image = '$_POST'");


    echo $result;
} else {
    $result = json_encode(array('receive' => 'nothing'));
    echo $result;
}

?>

这是我在数据库中得到的内容:

感谢您的帮助,我认为将图像保存在数据库中的方法是错误的...

【问题讨论】:

    标签: php angular ionic-framework blob


    【解决方案1】:

    我的解决方案是在客户端使用:base64 将您的图像转换为 base64,然后将其存储在服务器上。要再次查看,请将 base64 发送到客户端并使用 base64 插件再次转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      相关资源
      最近更新 更多