我到处寻找并尝试了很多修复来解决这个问题,但我很茫然,也许它只是我看不到的东西。我有一个带有 WCF 服务的 ASP.NET 网站,用于上传文件,我可以上传小文件(虽然 16kb 在另一端变成了 977)但是当我上传更大的文件(150kb)时,我得到一个错误远程服务器返回错误:(413)请求实体太大。我增加了可以上传的内容的最大大小,但据我所知,这就是我需要做的一切。所以我想你有两个问题:
- 为什么传输后文件变大了?
- 为什么我不能发送更大的文件而不会出现此错误?
我的 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)
{
}
}