HttpWebRequest复制大文件并找不到404

时间:2018-10-02 10:34:23

标签: c# http-status-code-404 httpwebrequest httpwebresponse

我没有问题地复制了小文件。 当用户尝试上传大尺寸文件(我猜> 60MB)时,我遇到了网络异常(找不到404)。

我非常确定问题是由于文件大小引起的。

我有例外
webRequest.GetResponse()

我需要修改服务器端吗? 任何建议都值得赞赏。

    public static bool UploadFile(IResult result, string pathFile, Stream stream)
    {
        // upload effettivo del file su DB
        HttpWebRequest webRequest = WebRequest.Create(ClientCommon.Properties.Settings.Default.FileServiceURI) as HttpWebRequest;
        HttpWebResponse response = null;
        Stream s = null;
        try
        {
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.KeepAlive = true;

            using (WebRequestBuilder wrb = new WebRequestBuilder())
            {
                webRequest.ContentType = wrb.ContentType;

                wrb.AddTextPart("cdFile", pathFile);
                wrb.AddFilePart("file", stream);
                wrb.AddTextPart("destination", pathFile);


                if (wrb.GetContent(result, out s) == false)
                    return false;

                s.CopyTo(webRequest.GetRequestStream());
            }

            response = webRequest.GetResponse() as HttpWebResponse;

            return true;
        }
        catch (WebException exc)
        {
            result.SetError(exc.Message);
            return false;
        }
        finally
        {
            if (response != null)
                response.Close();

            if (s != null)
                // When the above code has ended, close the streams
                s.Close();
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试在您的web.config中添加以下代码。

<system.web>
     <!-- Your other settings here -->
    <httpRuntime targetFramework="Your Framework" maxRequestLength="2147483647" />
</system.web>

<system.webServer>
    <!-- Your other settings here -->
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
</system.webServer>

maxRequestLength::属性maxRequestLength指示ASP.NET支持的最大文件上载大小。此限制可用于防止由于用户将大文件发布到服务器而导致的拒绝服务攻击。指定的大小以千字节为单位。默认值为4096 KB(4 MB)。 Here,您可以详细了解maxRequestLength

maxAllowedContentLength:指定对Web服务器处理的请求的限制。 Here,您可以详细了解maxAllowedContentLength

通过以上代码,您可以接收最大2GB的文件。您可以根据需要对其进行修改