2012-04-22 89 views
4

我到處尋找並嘗試了很多解決此問題的修復程序,但我不知所措,也許它只是我不能看到的東西。我有一個用於上傳文件的WCF服務的ASP.NET網站,我可以上傳小文件(雖然16KB原來是另一端的977),但是當我上傳一個較大的文件(150kb)時,我得到一個錯誤遠程服務器返回錯誤:(413)請求實體太大。我增加了可以上傳的內容的最大大小,但據我所知,這就是我所需要做的。所以我想你有2個問題:WCF上傳文件遠程服務器返回錯誤:(413)請求實體太大

  1. 爲什麼文件這麼大後,我發送它?
  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) 
    { 
    } 

} 

回答

3

變化

byte[] bytearray = new byte[1000000]; 

到:

byte[] bytearray = new byte[fileSize+1]; 
相關問題