我已經使用canvas.toBlob()函式將畫布元素轉換為 blob 資料,轉換為變數blob。然后我將它附加到formdata
const formData = new FormData();
formData.append('my-file',blob,fileName);
我使用 XMLHttpRequest 發送formData到storeImage.php
var sendImage = new XMLHttpRequest();
var url ="https://192.168.x.x/storeImage.php";
sendImage.open("POST",url, true);
sendImage.send(formData);
在里面storeImage.php我有這些代碼行至少嘗試首先訪問formData
<?php
var_dump($_POST['my-file'])
?>
這樣我以后可以將其存盤為檔案。
但結果var_dump如下。
Warning: Undefined array key "my-file" in E:\xampp\htdocs\wasp_bee\storeImage.php on line 2
NULL
我怎樣才能至少訪問 php 檔案中發布的 formData,所以我最終可以繼續使用該file_put_contents函式將 blob 資料作為檔案保存到服務器(./img)?
提前致謝 :)
uj5u.com熱心網友回復:
使用$_FILES陣列可以訪問二進制檔案。
通過 HTTP POST 方法上傳到當前腳本的專案的關聯陣列。該陣列的結構在 POST 方法上傳部分中進行了概述。
上傳的檔案可以使用該move_uploaded_file功能存盤在您的地址。
move_uploaded_file($_FILES['my-file']['tmp_name'], 'address to store file');
檢查檔案錯誤,名稱,格式等非常重要,請在主要參考中檢查它們。
https://www.php.net/manual/en/reserved.variables.files.php
https://www.php.net/manual/en/function.move-uploaded-file.php
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460490.html
