WCF REST 上传文件(某些文件成功 - 200,某些文件失败 - 400 错误请求)

发布于 2024-11-02 05:12:49 字数 1628 浏览 3 评论 0 原文

这是我的 WCF REST 端点:

[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;
    }
}

我正在使用这里的多部分解析器: http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

我的问题是上述对于某些文件(.bat、.txt、.cs、.doc)效果很好 - 我在 Fiddler 中看到了所有好的迹象,包括 200(OK)状态。

当我尝试上传其他文件(.xls、.vsd)时,失败并显示 400(错误请求)状态。我很惊讶 .doc 可以工作,而 .xls 和 .vsd 会失败。

它也是一致的。我已成功上传多个 .doc 文件,没有任何失败。我还尝试上传多个 .xls 文件 - 有些成功,有些失败(成功一次又一次一致,失败一次又一次一致)。当我编写本文并测试越来越多的文件时,有一个 .pdf 文件始终产生 504(Fiddler - 接收失败)错误。

仅供参考,我在客户端上使用 Flex 并使用 FileReference 类进行上传。 Flex 代码与它们一样标准 - 使用此代码唯一的变化是 WCF REST URL: http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/

为什么我看到一些失败的任何想法以及一些成功?我没看出两者有什么区别?

提前致谢。

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 client 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-11-09 05:12:49

您可以检查后续文件的大小,并调整 web.config 中 webHttpBinding 的 maxReceivedMessageSize。默认情况下只有 64KB。我也遇到过类似的问题,直到我将其提高了(这里将其乘以 1000)。还将 requestValidationMode 设置为 2.0 并将pages.validateRequest 设置为 false 以防止阻止“危险”上传。

这些更改让我的工作顺利进行,但我在处理超过 4MB 的文件时遇到了麻烦(无论 maxReceivedMessageSize 设置如何);解决这个问题需要增加 httpRuntime 的 maxRequestLength。

我将 TransferMode 设置为 StreamedRequest,但不确定以这种方式上传文件对 IIS 性能和/或拒绝服务攻击有何影响。我认为使用流模式应该相当安全。这是一篇关于 大数据和流媒体。我以前曾使用分块客户端来避免像这样的巨大请求。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文