我想在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;
}
}
?>
答案 0 :(得分:0)
为什么在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