作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将大文件上传到 IIS 中托管的 WCF 服务。
我正在使用 Restful 和 Streaming 方法。
但我无法上传超过 64KB 的文件。
我通过更改 web.config
中所有与尺寸相关的元素进行了很多尝试。文件,但未能解决问题。
这是我的代码和配置,如果有人在代码中发现任何问题以及如何修复,请告诉我。
经营契约(Contract)
[OperationContract]
[WebInvoke(UriTemplate = "/UploadImage/{filename}")]
bool UploadImage(string filename, Stream image);
public bool UploadImage(string filename, Stream image)
{
try
{
string strFileName = ConfigurationManager.AppSettings["UploadDrectory"].ToString() + filename;
FileStream fileStream = null;
using (fileStream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 1024;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = image.Read(buffer, 0, bufferLen)) > 0)
{
fileStream.Write(buffer, 0, count);
}
fileStream.Close();
image.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
<system.serviceModel>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
binding="webHttpBinding" bindingConfiguration="webBinding"
contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding"
transferMode="Streamed"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00"
receiveTimeout="00:25:00" >
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<httpRuntime maxRequestLength="2097151"/>
private static void UploadImage()
{
string filePath = @"F:\Sharath\TestImage\TextFiles\SampleText2.txt";
string filename = Path.GetFileName(filePath);
string url = "http://localhost/WCFService1/Service.svc/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "UploadImage/" + filename);
request.Accept = "text/xml";
request.Method = "POST";
request.ContentType = "txt/plain";
FileStream fst = File.Open(filePath, FileMode.Open);
long imgLn = fst.Length;
fst.Close();
request.ContentLength = imgLn;
using (Stream fileStream = File.OpenRead(filePath))
using (Stream requestStream = request.GetRequestStream())
{
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int byteCount = 0;
while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
{
requestStream.Write(buffer, 0, byteCount);
}
}
string result;
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
Console.WriteLine(result);
}
The remote server returned an error: (400) Bad Request
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
binding="webHttpBinding" bindingConfiguration="webBinding"
contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding transferMode="Streamed"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00"
name="webBinding">
<readerQuotas maxDepth="64"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
最佳答案
wcf 的大数据传输问题:
我在 IIS 7、Windows 2008 服务器上托管了 wcf 4.0 服务。当我用小数据调用我的服务时说 4K 或 5K 字节然后请求很容易处理但是在尝试上传大尺寸时它给了我以下错误
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBinding" closeTimeout="00:10:00" maxBufferPoolSize="250000000" maxReceivedMessageSize="250000000" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" messageEncoding="Text">
<readerQuotas maxDepth="4500000" maxStringContentLength="4500000" maxBytesPerRead="40960000" maxNameTableCharCount="250000000" maxArrayLength="4500000"/>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
<system.web>
<httpRuntime executionTimeout="4800" maxRequestLength="500000000"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="500000000"></requestLimits>
</requestFiltering>
</security>
</system.webServer>
<!-- other useful setting -->
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
关于上传大文件的 WCF 问题,托管在 IIS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6317794/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!