我正在使用此php脚本的ajax调用获得POST https://www.example.com/php/download.php 405(不允许)。它是为我编写的http文件服务器下载文件...使用php在web root之外抓取它们。我的其他PHP脚本/ ajax调用适用于我的http文件服务器...我只是通过这个特定的ajax调用/ php脚本得到了这个问题。我正在使用Nginx。
再次,只是为了emphisize ....在这个相同的服务器上其他ajax调用和其他PHP脚本工作。所以,有点失落。
<?php
$path = "/trunk/";
$file = $_POST['filename'];
$file_ext = pathinfo($file, PATHINFO_EXTENSION);
$file_name = pathinfo($file, PATHINFO_BASENAME);
$file_full = $path . $file_name;
$finfo = new finfo(FILEINFO_MIME);
$ctype = $finfo -> file($file_full);
header('Content-Disposition: attachment; filename=\"$file_name\"');
header("Content-Type: " .$ctype);
readfile($file_full);
?>
187
188 function downloadFile(event) {
189 var fname = event.target.getAttribute("href");
190 fname.replace("#","");
191 var form = new FormData();
192 form.append("filename", fname);
193
194 var xhr = new XMLHttpRequest();
195 xhr.onreadystatechange = function () {
196 // setTimeout(refresh, 500);
197 }
198
199 xhr.open("POST", "php/download.php", true);
200 xhr.send(form);
201 }
以下是来自Google的Chrome网络调试器:
Request URL:https://www.example.com/php/download.php
Request Method:POST
Status Code:405 Not Allowed
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:172
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryNGHvOaUnlBV8ADWE
Host:www.example.com
Origin:https://www.example.com
Referer:https://www.example.com/index.php
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Request Payload
------WebKitFormBoundaryNGHvOaUnlBV8ADWE
Content-Disposition: form-data; name="filename"
CentOS-6.3-x86_64-minimal-EFI.iso
------WebKitFormBoundaryNGHvOaUnlBV8ADWE--
Response Headersview source
Connection:keep-alive
Content-Length:568
Content-Type:text/html
Date:Fri, 30 Nov 2012 23:59:59 GMT
Keep-Alive:timeout=300
Server:nginx
答案 0 :(得分:2)
你的nginx配置,检查一下。您的位置块不允许您访问该文件。
答案 1 :(得分:1)
我忘了在nginx.conf中我在location块中单独指定了每个php文件。我忘了将download.php文件添加到其中。感谢Jacob和Guilherme试图提供帮助!
答案 2 :(得分:0)
<强>修正:强>
header('Content-Disposition: attachment; filename="'.$file_name.'"');
<强>修正:强>
xhr.onreadystatechange = function () {
if (r.readyState===4 && r.status===200) {
//setTimeout(refresh, 500);
}
}
<强> [编辑] 强>
<强>尝试:强>
xhr.open("POST", "php/download.php", true);
xhr.onreadystatechange = function () {
...
}
xhr.send(form);