Вопрос

I am getting a POST https://www.example.com/php/download.php 405 (Not Allowed) with this ajax call to my php script. It is for a http file server that I wrote to download files...grab them outside of web root using php. My other PHP scripts/ajax calls work for my http file server...I am only getting this issue with this particular ajax call/php script. I am using Nginx.

Again, just to emphisize....On this same server other ajax calls and other php scripts work. So, kinda lost.

<?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 }

Here is from Google's chrome network debugger:

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
Это было полезно?

Решение

Your nginx config, check it. Your location blocks aren't allowing you to access that file.

Другие советы

I forgot that in nginx.conf I specified each php file individually in the location block. I forgot to add the download.php file into it. Thanks Jacob and Guilherme for trying to help!

Fix:

header('Content-Disposition: attachment; filename="'.$file_name.'"');

Fix:

     xhr.onreadystatechange = function () {
        if (r.readyState===4 && r.status===200) {
           //setTimeout(refresh, 500);
        }
     }

[edited]

Try:

xhr.open("POST", "php/download.php", true);
xhr.onreadystatechange = function () {
    ...
}
xhr.send(form);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top