gpt4 book ai didi

javascript - 发送文件返回405(方法不允许)

转载 作者:行者123 更新时间:2023-12-03 04:19:41 26 4
gpt4 key购买 nike

客户端向服务器发送文件。使用 XMLHttpRequest() 发出的请求。我使用 PHP 作为服务器。在浏览器中运行时,从文件 return this error 上传405(不允许的方法)。所需的 header 已存在于 PHP 代码中。我不明白为什么它不起作用

PHP

<?php                                                         
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, PUT');
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] :
false);

if ($fn) {

// AJAX call
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();

}
else {

// form submit
$files = $_FILES['fileselect'];

foreach ($files['error'] as $id => $err) {
if ($err == UPLOAD_ERR_OK) {
$fn = $files['name'][$id];
move_uploaded_file(
$files['tmp_name'][$id],
'uploads/' . $fn
);
echo "<p>File $fn uploaded.</p>";
}
}
}

JavaScript

    function UploadFile(file) {

// following line is not necessary: prevents running on SitePoint servers
if (location.host.indexOf("sitepointstatic") >= 0) return

var xhr = new XMLHttpRequest();
if (xhr.upload && file.type == "image/jpeg" && file.size <= $id("MAX_FILE_SIZE").value) {

// create progress bar
var o = $id("progress");
var progress = o.appendChild(document.createElement("p"));
progress.appendChild(document.createTextNode("upload " + file.name));


// progress bar
xhr.upload.addEventListener("progress", function(e) {
var pc = parseInt(100 - (e.loaded / e.total * 100));
progress.style.backgroundPosition = pc + "% 0";
}, false);

// file received/failed
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4) {
progress.className = (xhr.status == 200 ? "success" : "failure");
}
};

// start upload
xhr.open("POST", $id("upload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);//This is line 116

}

}

第41行是调用UploadFile(f);

最佳答案

请参阅 php.ini 配置(在服务器上),如文档中所述。有多种上传文件的选项。您可以在 php.net 上找到它,例如

关于javascript - 发送文件返回405(方法不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com