Http File Upload

Topics: Web Api
Sep 16, 2011 at 8:41 PM

I have been testing the new File Upload feature and it appears to limit the file size to under 100 kb. For example uploading a 53Kb file I get this response:

Key Value
Response HTTP/1.1 200 OK
Cache-Control private
Content-Length 287
Content-Type application/xml; charset=utf-8
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:36:12 GMT

------------------------------------------------------------

When I try a 128 Kb file I get this response:

Key Value
Response HTTP/1.1 400 Bad Request
Cache-Control private
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:37:48 GMT
Content-Length 0

------------------------------------------------------------------

Changing the maximum allowable request length in the web.config file does not appear to make any difference. Both file were PDF extension.

Sep 16, 2011 at 9:24 PM

You can control the max message size using the new HttpConfiguration object (default is 64k). Something like this will set the max size of the incoming request to 1M and also turn on streaming so that you avoid having to keep it all in memory:

HttpConfiguration config = new HttpConfiguration

{

TransferMode = TransferMode.StreamedRequest,

MaxReceivedMessageSize = 1024 * 1024

};

fileServiceHost = new HttpServiceHost(typeof(FileService), config, fileServiceAddress);

If you are hosted in IIS/ASP you also may also have to set the maxRequestLength property in the web.config file so that it matches the MaxReceivedMessageSize value. I think the default size is 4M so you have some more leeway there.

If you want to upload more than 2G file sizes then you have to host Web API in selfhost as maxRequestLength maxes out at 2G. However, MaxReceivedMessageSize is a long so there is plenty of space.

Henrik

From: aloepp [email removed]
Sent: Friday, September 16, 2011 1:42 PM
To: Henrik Frystyk Nielsen
Subject: Http File Upload [wcf:272885]

From: aloepp

I have been testing the new File Upload feature and it appears to limit the file size to under 100 kb. For example uploading a 53Kb file I get this response:

Key Value
Response HTTP/1.1 200 OK
Cache-Control private
Content-Length 287
Content-Type application/xml; charset=utf-8
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:36:12 GMT

------------------------------------------------------------

When I try a 128 Kb file I get this response:

Key Value
Response HTTP/1.1 400 Bad Request
Cache-Control private
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:37:48 GMT
Content-Length 0

------------------------------------------------------------------

Changing the maximum allowable request length in the web.config file does not appear to make any difference. Both file were PDF extension.

Sep 19, 2011 at 5:32 PM
Thank you. This helped a lot
Alex
From: [email removed]
Sent: Friday, September 16, 2011 3:24 PM
To: [email removed]
Subject: Re: Http File Upload [wcf:272885]

From: HenrikN

You can control the max message size using the new HttpConfiguration object (default is 64k). Something like this will set the max size of the incoming request to 1M and also turn on streaming so that you avoid having to keep it all in memory:

HttpConfiguration config = new HttpConfiguration

{

TransferMode = TransferMode.StreamedRequest,

MaxReceivedMessageSize = 1024 * 1024

};

fileServiceHost = new HttpServiceHost(typeof(FileService), config, fileServiceAddress);

If you are hosted in IIS/ASP you also may also have to set the maxRequestLength property in the web.config file so that it matches the MaxReceivedMessageSize value. I think the default size is 4M so you have some more leeway there.

If you want to upload more than 2G file sizes then you have to host Web API in selfhost as maxRequestLength maxes out at 2G. However, MaxReceivedMessageSize is a long so there is plenty of space.

Henrik

From: aloepp [email removed]
Sent: Friday, September 16, 2011 1:42 PM
To: Henrik Frystyk Nielsen
Subject: Http File Upload [wcf:272885]

From: aloepp

I have been testing the new File Upload feature and it appears to limit the file size to under 100 kb. For example uploading a 53Kb file I get this response:

Key Value
Response HTTP/1.1 200 OK
Cache-Control private
Content-Length 287
Content-Type application/xml; charset=utf-8
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:36:12 GMT

------------------------------------------------------------

When I try a 128 Kb file I get this response:

Key Value
Response HTTP/1.1 400 Bad Request
Cache-Control private
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?RDpcUHJvamVjdHNcRGF0YUxvYWRlclNlcnZpY2VcRGF0YUxvYWRlclNlcnZpY2VcU2VydmljZTFcRmlsZURhdGE=?=
X-Powered-By ASP.NET
Date Fri, 16 Sep 2011 20:37:48 GMT
Content-Length 0

------------------------------------------------------------------

Changing the maximum allowable request length in the web.config file does not appear to make any difference. Both file were PDF extension.