我有一个使用分块编码接收文件的 Windows/Apache2/PHP 应用程序。原因是上传的文件是动态的,在传输之前它的长度是未知的。这一直开箱即用。
现在我需要将应用程序移植到 IIS7/PHP。问题是 IIS 无法接收分块文件:当文件上传时,服务器根本没有响应。我该如何解决这个问题?
请注意,在我的测试中,我什至不使用 PHP。我只是有一个 .php 扩展名,因为 IIS 拒绝 .htm 文件上的 POST(这是有道理的)。
正如 rupello 在这个答案中所建议的那样,我使用 cURL 进行了测试,以确保我的客户端没有损坏。cURL 也无法得到答案,尽管如果传输没有分块,一切正常。
我做了以下测试:
测试.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
</head>
<body>
<form method="post" enctype="multipart/form-data">
File: <input type="file" name="upfile" />
<input type="submit" value="go"/>
</form>
</body>
</html>
此命令不返回(卡在等待答案)
curl.exe http://serveur/test.php --form "upfile=@text.txt"
-H "Transfer-Encoding: chunked" -H "Expect:"
注意:-H "Expect:"
是抑制Expect 100-Continue
curl发出的。没有这个头的结果是一样的,当然除了额外的往返。发送:
POST http://serveur/test.php HTTP/1.1
User-Agent: curl/7.15.3 (i586-pc-mingw32msvc) libcurl/7.15.3 zlib/1.2.2
Host: serveur
Pragma: no-cache
Accept: */*
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: multipart/form-data; boundary=----------------------------310dbcc6761b
8c
------------------------------310dbcc6761b
Content-Disposition: form-data; name="upfile"; filename="text.txt"
Content-Type: text/plain
5
hello
30
------------------------------310dbcc6761b--
0
问题:服务器没有返回任何内容。服务器看起来一直在等待。卷曲不返回。
没有块编码的相同命令按预期工作:
发送:
POST http://serveur/test.php HTTP/1.1
User-Agent: curl/7.15.3 (i586-pc-mingw32msvc) libcurl/7.15.3 zlib/1.2.2
Host: serveur
Pragma: no-cache
Accept: */*
Connection: Keep-Alive
Content-Length: 193
Content-Type: multipart/form-data; boundary=----------------------------e2d761bc173a
------------------------------e2d761bc173a
Content-Disposition: form-data; name="upfile"; filename="text.txt"
Content-Type: text/plain
hello
------------------------------e2d761bc173a--
服务器现在正确回复:
HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.8
X-Powered-By: ASP.NET
Date: Mon, 21 Nov 2011 10:47:57 GMT
Content-Length: 272
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
</head>
<body>
<form method="post" enctype="multipart/form-data">
File: <input type="file" name="upfile" />
<input type="submit" value="go"/>
</form>
</body>
</html>
具有相同文件和请求的普通 LAMP 服务器上的测试工作正常。
那么如何在 IIS 上启用请求块编码呢?
注意:我确实尝试了相关的 ASP 参数,但无济于事:
C:\...\inetsrv>appcmd.exe set config /section:asp /enableChunkedEncoding:True
Applied configuration changes to section "system.webServer/asp" for "MACHINE/
WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST"