开发者

WCF REST Upload File (some files succeed - 200, some fail - 400 Bad Request)

Here's my WCF REST endpoint:

[WebInvoke(Method = "POST", UriTemplate = "_test/upload")]
public void UploadImage(Stream data)
{
    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
    try
    {
        var parser = new MultipartParser(data);
        var ext = Path.GetExtension(parser.Filename);
        var filename = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), ext);
        var folder = HttpContext.Current.Server.MapPath(@"~\Uploads\");
        var filepath = Path.Combine(folder, filename);
        File.WriteAllBytes(filepath, parser.FileContents);
    }
    catch (Exception)
    {
        WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
    }
}

And I'm using the multipart parser from here: http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

My issue is that the above works great for some files (.bat, .txt, .cs, .doc) - I see in Fiddler all the good signs including the 200 (OK) status.

When I try to upload other files (.xls, .vsd), it fails with a 400 (Bad Request) status. I'm very surprised that a .doc would work and a .xls and .vsd would fail.

It is consistent as well. I've uploaded several .doc files successfully without any failures. I've also tried to upload several .xls files - some succeed, some fail (the successes are consistent over and over, the failures are consistent over and over). As I write this and test more and more files, there is a .pdf file that consistently produces a 504 (Fiddler - Receive Failure) error.

FYI, I am using Flex on the clien开发者_运维技巧t and using the FileReference class to do the uploads. The Flex code is as standard as they come - using this code with the only change being the WCF REST URL: http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/

Any ideas why I am seeing some failures and some successes? I don't see the difference between the two?

Thanks in advance.


You might check the sizes of the files that are succeeding and adjust the maxReceivedMessageSize of your webHttpBinding in web.config. It is only 64KB by default. I was experiencing a similar issue until I raised it way up (here multiplying it by 1000). Also set requestValidationMode to 2.0 and pages.validateRequest to false to prevent blocking of "dangerous" uploads.

These changes got things working for me but I ran into trouble with files over about 4MB (regardless of maxReceivedMessageSize setting); fixing that required increasing the maxRequestLength for the httpRuntime.

I set transferMode to StreamedRequest, but am not sure what the implications of uploading files this way are for IIS performance and/or denial-of-service attacks. I think it should be fairly safe with Streaming mode. Here's a decent MSDN article on Large Data and Streaming. I have previously used chunking clients to avoid huge requests like this.

<system.web>
    <httpRuntime requestValidationMode="2.0" maxRequestLength="65536000" />
    <pages validateRequest="false" />

<!-- (etc.) -->
</system.web>
<!-- (etc.) -->
<system.serviceModel>
     <bindings>
         <webHttpBinding>
             <binding maxReceivedMessageSize="65536000" transferMode="StreamedRequest">
                <security mode="None" />
              </binding>
         </webHttpBinding>
    </bindings>
</system.serviceModel>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜