我的離子應用程序有時在瀏覽器中使用,我需要上傳一些圖像。我已經實現了一個代碼來選擇我的文件,然后我有一個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>文件: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;}?>這是我在數據庫中得到的:感謝您的幫助,我認為將我的圖像保存在我的數據庫中是錯誤的方式......
1 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
我的解決方案是在客戶端使用 base64 將您的圖像轉換為base64?,然后將其存儲在服務器上。要再次查看,請將 base64 發送到客戶端并使用 base64 插件再次轉換。
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消