各位大神,遇到个问题,几天了解决不了愁得我吃不下饭睡不了觉呀···
wcf上传文件,一超过48k就是400 bad request
以下为代码:
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void UploadFile(FileData file);
}
[MessageContract]
public class FileData
{
[MessageHeader]
public string filename;
[MessageBodyMember]
public Stream data;
}
}
namespace WcfService1 { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。 public class Service1 : IService1 {
public void UploadFile(FileData file) { FileStream fs = new FileStream(@"C:\"+file.filename, FileMode.OpenOrCreate); try {
BinaryReader reader = new BinaryReader(file.data); byte[] buffer; BinaryWriter writer = new BinaryWriter(fs); long offset = fs.Length; writer.Seek((int)offset, SeekOrigin.Begin); do { buffer = reader.ReadBytes(1024);
writer.Write(buffer);
} while (buffer.Length > 0); } catch (Exception e) {
} finally {
fs.Close(); file.data.Close();
} } } }
apconfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="http://localhost:51094/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="LargeDataTransferServicesBinding" contract="WcfService1.IService1" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647" messageEncoding="Mtom" transferMode="Streamed" sendTimeout="00:10:00">
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
客户端:
namespace WebApplication8 { public partial class _Default : System.Web.UI.Page {
protected void Button1_Click(object sender, EventArgs e) { ServiceReference1.Service1Client service = new ServiceReference1.Service1Client(); service.UploadFile(FileUpload1.PostedFile.FileName, FileUpload1.FileContent); } } }
webconfig:
<system.serviceModel> <bindings> <basicHttpBinding>
<binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" messageEncoding="Mtom" transferMode="Streamed" maxReceivedMessageSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> <security mode="None" /> </binding>
</basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:51094/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /> </client> </system.serviceModel>
求指点,急急急····
拜托你格式化下代码,这样的代码看起来很累,知道吗?
另外说明下你的网络条件,传输48K文件所用的时间留意下,是不是正好1分种,有个一分钟超时设置的。还有就是网络是否丢包,如果会丢包,文件大了也会导致失败。
我记得应该是64KB的限制啊,可以用流来传送大文件。
需要注意你的Server端和Client端的包大小设置要一样,如果不一样会出现400 Bad Request的问题
楼主,这个问题解决了吗?