我的離子應(yīng)用程序有時(shí)在瀏覽器中使用,我需要上傳一些圖像。我已經(jīng)實(shí)現(xiàn)了一個(gè)代碼來(lái)選擇我的文件,然后我有一個(gè)formData類型可以放入我的圖像。我正在嘗試使用發(fā)布請(qǐng)求將我的文件上傳到服務(wù)器,但我不知道該怎么做。當(dāng)我在我的 php 代碼中收到我的文件時(shí),我將它插入到我的數(shù)據(jù)庫(kù)中,我的數(shù)據(jù)庫(kù)中有一個(gè) blob 文件,但我認(rèn)為它不是圖像,它太小了......html文件: <input style="display: none" type="file" (change)="onFileChanged($event)" #fileInput><button (click)="fileInput.click()">Select File</button><button (click)="onUpload()">Upload!</button>文件: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 文件:<?phpheader('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;}?>這是我在數(shù)據(jù)庫(kù)中得到的:感謝您的幫助,我認(rèn)為將我的圖像保存在我的數(shù)據(jù)庫(kù)中是錯(cuò)誤的方式......
在離子應(yīng)用程序中從計(jì)算機(jī)中選擇圖像以將其提供給服務(wù)器
夢(mèng)里花落0921
2023-04-28 15:28:04