使用HttpWebRequest向servicestack服务发送大型(8mb)excel文件时出错

时间:2015-09-08 10:10:27

标签: c# asp.net rest servicestack

我正在尝试将大型excel文件发送到其余服务(使用servicestack)。

客户端和服务器(Servicestack服务)应用程序部署在不同的服务器上。

我在客户端使用以下代码上传excel文件(8 MB大小)

取自

http://www.codeproject.com/Articles/501608/Sending-Stream-to-ServiceStack

 HttpWebRequest client = (HttpWebRequest)WebRequest
                 .Create(ConfigurationManager
                 .AppSettings["applicationUrl"] 
                   + @"/servicename/" 
                   + fileUpload.FileName 
                   + @"/" + FileType + @"/" 
                   + Utility.UserName + @"/" 
                   + Utility.EmployerID + @"/" 
                   + UploadedFrom);

 client.Method = WebRequestMethods.Http.Post;  

 ////the following 4 rows enable streaming 
  client.AllowWriteStreamBuffering = false;
  client.SendChunked = true;
  client.ContentType = "multipart/form-data;";
  client.Timeout = int.MaxValue;
   // client.KeepAlive = false;
  //client.ProtocolVersion = HttpVersion.Version10;
 //client.ContentLength = 0;
 //client.Timeout = Timeout.Infinite;
//client.AllowWriteStreamBuffering = true;
Stream stream = fileUpload.InputStream;

if (stream.CanRead)  
{  stream.Copy(client.GetRequestStream());
}

   var response = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();

上面的代码在上传的文件是2-3 MB时工作正常。 当文件大小约为8MB时,它会抛出错误 在调试时它会给我以下错误

System.Net.WebException was caught
  HResult=-2146233079
  Message=The remote server returned an error: (500) Internal Server Error.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at Myproject.Web.Controllers
                  .Employer.FileUploadController
                  .Upload(FileUploadViewModel fileUploadViewModel
        ,HttpPostedFileBase fileUpload
        ,String DataFileType
        ,String UploadedFrom) 
    in d:\MyProject\projNewArch\Controllers\Employer\FileUploadController.cs:line 262
  InnerException:

我尝试更改可以看作已连接的设置值。

可能是什么问题?请帮忙

更新1:我在服务器上有IIS 8.5,所以我认为文件大小(8mb)可能不是问题。我是否仍需要在web.config中添加一些设置以允许通过POST发送大尺寸文件

更新2:找到解决方案。 添加maxAllowedContentLength和maxRequestLength就可以了。请在评论部分

中查看Smartdev提供的链接

1 个答案:

答案 0 :(得分:1)

您仍需要编辑web.config文件。它应该是这样的:

<system.webServer>
<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="10485760" /> 
  </requestFiltering>
</security>

这将允许您上传最多10MB