[英]xmlHTTP POST request
我環顧四周stackoverflow和在google上,但到目前為止找不到有效的解決方案。
我正在嘗試通過xmlhttp請求將圖像文件發送到服務器。 我找到了這個網站: https : //ptsv2.com/以測試上傳。
發送請求時,我從服務器得到200-確定,但它還顯示沒有文件上傳(文件:0)
這是我的代碼:
var url = "https://corsanywhere.herokuapp.com/https://ptsv2.com/t/zuaco-1549007477/post";
var base64Credentials = btoa(username+":"+password);
var xhttp = new XMLHttpRequest();
var fd = new FormData();
/* Add the file */
fd.append("file", "img/1.png");
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Authorization", "Basic " + base64Credentials);
xhttp.setRequestHeader("Content-Type", "image/png");
xhttp.send(fd);
/* Check the response status */
xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
console.log("UPLOAD SUCCESSFUL: " + xhttp.statusText);
console.log("GET ALL: " + xhttp.getAllResponseHeaders());
}
else {
console.log("UPLOAD FAILED!")
}
}
出於安全原因,您無法手動設置文件。 而是使用文件輸入元素:
var xhttp = new XMLHttpRequest();
var fd = new FormData();
// fd.append("file", "img/1.png");
fd.append("file", document.getElementById('yourInput').files[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.