【问题标题】:How to upload file using PHP and HTML in Ionic?如何在 Ionic 中使用 PHP 和 HTML 上传文件?
【发布时间】:2018-08-21 21:40:44
【问题描述】:

我想在 Ionic 中使用这个 HTML 和 PHP 代码来上传图像。如何在 Ionic 中使用此代码?

HTML

<form action="http://sco7.com/del/uploadtest/uploadionic.php" method="post" enctype="multipart/form-data">  
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

PHP

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

【问题讨论】:

  • 您可以使用文件传输

标签: php angular ionic-framework ionic2


【解决方案1】:

为什么在 IONIC 应用程序中使用 PHP 代码?
使用 http POST 方法通过 API 上传文件
下面是一个简单的例子
在您的应用中安装插件

$ ionic cordova plugin add cordova-plugin-advanced-http
$ npm install --save @ionic-native/http  

将其导入您的应用中

import { HTTP } from '@ionic-native/http';

在构造函数中初始化

constructor(public http: HTTP){}

现在您可以发出 POST 请求

this.http.psot("your/api/endpoint", {"fileName":"filedata"}, {"Authorization":"some auth"})
.then(()=>{ console.log('file uploaded')});  

您可能希望将文件数据转换为 Base64 或 Blob

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多