WCF上载文件远程服务器返回错误:(413)请求实体太大

我到处寻找并尝试了很多修复来解决这个问题,但我不知所措,也许它只是我看不到的东西.我有一个带有WCF服务的ASP.NET网站用于上传文件,我可以上传小文件(虽然16kb在另一端变成了977)但是当我上传一个更大的文件(150kb)时,我得到了一个error远程服务器返回错误:(413)请求实体太大.我增加了可以上传的内容的最大大小,但据我所知,这就是我需要做的全部内容.所以我想你有2个问题:

>为什么传输后文件会大得多?
>为什么我不能发送更大的文件而不会收到此错误?

我的Web服务很好,但为了它,这里是代码:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}")]
void FileUpload(string fileName, Stream fileStream);

    public void FileUpload(string fileName, Stream fileStream)   
     {   
                 FileStream fileToupload = new FileStream("C:\\" + fileName, FileMode.Create);

        byte[] bytearray = new byte[1000000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        fileToupload.Write(bytearray, 0, bytearray.Length);
        fileToupload.Close();
        fileToupload.Dispose();
     }   

我的web.config文件如下

<system.serviceModel>
<bindings>
    <basicHttpBinding>
        <binding name="uploadfilebinding" closeTimeout="10:01:00"
          maxBufferSize="204857600" maxBufferPoolSize="204857600"
          maxReceivedMessageSize="104857600" openTimeout="10:01:00"
          receiveTimeout="10:10:00" sendTimeout="10:01:00"
          messageEncoding="Mtom" transferMode="StreamedRequest">
            <readerQuotas maxDepth="204857600" maxStringContentLength="204857600"
                          maxArrayLength="204857600" maxBytesPerRead="204857600"
                          maxNameTableCharCount="204857600" />
        </binding>
    </basicHttpBinding>
</bindings>
<services>
    <service name="sUploadFile.UploadFile"
             behaviorConfiguration="uploadfilebehavior">
        <endpoint
              address=""
              binding="basicHttpBinding"
              bindingConfiguration="uploadfilebinding"
              contract="sUploadFile.UploadFile">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
        <endpoint
              address="mex"
              binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
</services>

<behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        <behavior name="uploadfilebehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>

    </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

我打电话给它如下:

string baseServiceAddress = "http://" + Environment.MachineName + ":8000/suploadfile.svc/FileUpload";
ServiceHost host = new ServiceHost(typeof(sUploadFile), new Uri(baseServiceAddress));
host.AddServiceEndpoint(typeof(IsUploadFile), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
host.Open();

 byte[] bytearray=null ;
      string name = "";
    //throw new NotImplementedException();
    if (FileUpload1.HasFile)
    {
         name = FileUpload1.FileName;
        Stream stream = FileUpload1.FileContent;
        stream.Seek(0, SeekOrigin.Begin);
        bytearray = new byte[stream.Length];
        int count = 0;
        while (count < stream.Length)
        {
            bytearray[count++] = Convert.ToByte(stream.ReadByte());
        }

    }

    string baseAddress = "http://" + Environment.MachineName + ":8000/suploadfile.svc/FileUpload/";
    HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(baseAddress+name);          
    request.Method = "POST";
    request.ContentType = "text/plain";
    try
    {
        Stream serverStream = request.GetRequestStream();
        serverStream.Write(bytearray, 0, bytearray.Length);
        serverStream.Close();
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            int statusCode = (int)response.StatusCode;
            StreamReader reader = new StreamReader(response.GetResponseStream());

        }
    }
    catch (Exception ess)
    {
    }

}
最佳答案
更改

byte[] bytearray = new byte[1000000];

至:

byte[] bytearray = new byte[fileSize+1];

转载注明原文:WCF上载文件远程服务器返回错误:(413)请求实体太大 - 代码日志