2015-10-13 135 views
0

我试图实现文件上传到我的WCF服务在ASP.NET的C#文件上传(413)请求实体过大

下面是文件上传在WCF服务器的代码。

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

     // byte[] bytearray = new byte[10000]; 
      byte[] bytearray = new byte[1000]; 
      int bytesRead, totalBytesRead = 0; 
      do 
      { 
       bytesRead = fileStream.Read(bytearray, 0, bytearray.Length); 
       totalBytesRead += bytesRead; 
       if(bytesRead > 0) 
       fileToupload.Write(bytearray, 0, bytearray.Length); 

      } while (bytesRead > 0); 

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



     } 

下面是客户端上传文件的代码:(固定的字节数组大小)

protected void bUpload_Click(object sender, EventArgs e) 
{ 
    byte[] bytearray = null; 
    Stream stream; 
    string fileName = ""; 
    //throw new NotImplementedException(); 
    if (FileUpload1.HasFile) 
    { 
     fileName = FileUpload1.FileName; 
     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://localhost/WCFService/Service1.svc/FileUpload/"; 

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + fileName); 
    request.Method = "POST"; 
    request.ContentType = "text/plain"; 
    Stream serverStream = request.GetRequestStream(); 
    serverStream.Write(bytearray, 0, bytearray.Length); 
    serverStream.Close(); 
    try 
    { 
     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
     { 
      int statusCode = (int)response.StatusCode; 
      System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode); 

      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString()); 

     } 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---"); 
     ex.ToString(); 
    } 
} 

这是工作好具有体积小文件,当我和大大小的文件试过 我将固定大小的字节数组更改为动态字节数组写入流。 这里是更新的代码:(1024个字节的字节数组数据块用于发送数据)

 request.Method = "POST"; 
     request.ContentType = "text/plain"; 
     // Stream serverStream = request.GetRequestStream(); 

     if (FileUpload1.HasFile) 
     { 
      fileName = FileUpload1.FileName; 
      stream = FileUpload1.FileContent; 
      stream.Seek(0, SeekOrigin.Begin); 
      bytearray = new byte[1024];//stream.Length]; 


     } 
     int TbyteCount = 0; 
     Stream requestStream = request.GetRequestStream(); 

      int bufferSize = 1024; 
      byte[] buffer = new byte[bufferSize]; 
      int byteCount = 0; 
      while ((byteCount = stream.Read(buffer, 0, bufferSize)) > 0) 
      { 
       TbyteCount = TbyteCount + byteCount; 
       requestStream.Write(buffer, 0, byteCount); 
      } 

      requestStream.Close(); 


     try 
     { 
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
      { 
       int statusCode = (int)response.StatusCode; 
       System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode); 

       StreamReader reader = new StreamReader(response.GetResponseStream()); 
       System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString()); 

      } 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---"); 
      ex.ToString(); 
     } 
    } 

但是在读响应我得到异常 远程服务器返回一个错误:(413)请求实体过大。

我是否正确地在请求流中写入多次!

我使用的文件大小22.4 KB,则使用第一代码(固定大小的数组) 成功上传如果我在1024个字节的倍数分割文件的大小,并试图发送再有一个问题。

Web.config文件

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="WcfServiceApp.Service1" behaviorConfiguration="ServiceBehavior"> 
     <endpoint address="" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:50327/Service1.svc"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="webBehaviour"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <!--<protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping>--> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*"/> 
     <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/> 
     </customHeaders> 
    </httpProtocol> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 
+0

http://stackoverflow.com/questions/10122957/iis7-413-request-entity-too-large-uploadreadaheadsize – dremerDT

+0

感谢您的评论。我使用的文件大小为22.4 KB,使用第一个代码(固定大小的数组)成功上传。如果我将文件大小以1024字节的倍数分割并尝试发送,则会出现问题。 – Ashok

+0

您似乎没有将数据以块的形式发送到服务 - 您正在以块的形式构建流,但是您将它作为一个请求发送。你可以发布你的服务的绑定配置(最好是整个配置的部分)。您可能没有创建特定的绑定,也没有将所述绑定分配给显式端点。 – Tim

回答

1

当您尝试传输大系列化对象一些结构这个问题通常是由配置maxItemsInObjectGraph泄漏引起的。看我answer here

但在你的情况你试试只是传输简单的数据文件作为字节流。要通过webHttpBinding来做到这一点,你应该指定合适的服务合同,它接受只有流消息作为输入。所有额外的东西,如文件名,你可以指定为消息合约中的头文件(实际上也许你使用URI作为参数的方式也可以)。然后,您必须为您的绑定设置TransferMode = TransferMode.Streamed。一些code example is here多一个with config samples is here

关键词额外的谷歌搜索是的WebHttpBinding流

+0

我已经添加webhttpbinding,它的工作。感谢您的链接 – Ashok

相关问题