简体   繁体   English

javascript对象的XMLHttpRequest setRequestHeader方法不起作用

[英]javascript object's XMLHttpRequest setRequestHeader method doesn't work

I have a problem with file uploading. 我的文件上传有问题。 I'm using that very method described in THIS article. 我使用的描述非常方法文章。 I downloaded the whole script published by the author from HERE . 我从HERE下载了作者发布的整个脚本。
The application is meant to load files through HTML5 drag&drop and then, through javascript, send them to serverside by ajax request. 该应用程序旨在通过HTML5拖放加载文件,然后通过javascript,通过ajax请求将它们发送到服务器端。
Everything works fine but the problem occurs when I want to read a parameter from AJAX request header. 一切正常但当我想从AJAX请求头读取参数时会出现问题。 Here's the code of PHP "file reader": 这是PHP“文件阅读器”的代码:

$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);

if ($fn) {
//above there is the upload of the file with file_put_contents function
//which actually works fine when I replace $fn with my own value and ommit the "if" condition

Earlier, the XMLHttpRequest.setRequestHeader method is launched to set "X_FILENAME" header. 之前,启动XMLHttpRequest.setRequestHeader方法以设置“X_FILENAME”标头。 Here's the javascript: 这是javascript:

var xhr = new XMLHttpRequest();

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

And here's the proof (a crop from my Chrome's "firebug"): 这是证据(来自我Chrome的“萤火虫”的作物):

When I var_dump the $fn variable in PHP it returns boolean FALSE. 当我在PHP中var_dump $ fn变量时,它返回布尔值FALSE。 What is wrong? 怎么了?

PS I'm using XAMPP v1.8.1 with Apache 2.4.3 and PHP 5.4.7 on Win7 x64. PS我在Win7 x64上使用XAMPP v1.8.1与Apache 2.4.3和PHP 5.4.7。 I'm running the site on the latest Chrome. 我在最新的Chrome上运行该网站。 As you can guess the site is running on localhost. 您可以猜测该站点正在localhost上运行。 I didn't change anything in php.ini file - everything is set to default. 我没有更改php.ini文件中的任何内容 - 一切都设置为默认值。

Underscores don't seem to be valid characters for header names. 下划线似乎不是标题名称的有效字符。 Use Hyphens and it will work beautifully. 使用连字符,它将工作得很漂亮。

xhr.setRequestHeader("X-FILENAME", file.name);

and no changes are required in your PHP. 并且PHP中不需要进行任何更改。

New versions of Apache and PHP 5.3 新版本的Apache和PHP 5.3

   $UploadDir=dirname(__FILE__).'/'.;
   $Datos_cabecera=getallheaders();
    $fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
    if (!$fn) {$fn=$Datos_cabecera['X_FILENAME'];} //New versions of Apache and PHP 5.3
    if ($fn) {
    file_put_contents($UploadDir.$fn,file_get_contents('php://input'));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM