gpt4 book ai didi

WCF上传文件远程服务器返回错误: (413) Request Entity Too Large

转载 作者:行者123 更新时间:2023-12-01 22:29:49 25 4
gpt4 key购买 nike

我到处寻找并尝试了很多修复来解决这个问题,但我很茫然,也许这只是我看不到的东西。我有一个带有 WCF 服务的 ASP.NET 网站,用于上传文件,我可以上传小文件(尽管 16kb 在另一端结果是 977),但是当我上传更大的文件(150kb)时,我得到一个错误 远程服务器返回错误:(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)
{
}

}

最佳答案

改变

byte[] bytearray = new byte[1000000];

至:

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

关于WCF上传文件远程服务器返回错误: (413) Request Entity Too Large,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com