我想通过Web服务(REST - WCF)将文件上传到数据库,但我有WebException(400)错误的请求,我读了很多解决方案,但我的代码仍无效!
的.config
<system.serviceModel>
<bindings>
<webHttpBinding>
<!--Limits to 10MB-->
<binding name="ApiQuotaBinding"
maxReceivedMessageSize="1048576000"
maxBufferPoolSize="1048576000"
maxBufferSize="1048576000"
closeTimeout="00:03:00"
openTimeout="00:03:00"
receiveTimeout="00:03:00"
sendTimeout="00:03:00"
>
<readerQuotas maxDepth="32"
maxStringContentLength="104857600"
maxArrayLength="1048576000"
maxBytesPerRead="1048576000"
/>
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="TransferService">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration="ApiQuotaBinding"
contract="ITransferService"
behaviorConfiguration="webHttpBehavior"/>
<endpoint address="mex"
contract="IMetadataExchange"
binding="mexHttpBinding"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior >
<!--To avoid disclosing metadata information, set the values below to false before deployment-->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!--To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information-->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
这是我的网络方法:
public Guid UploadFile(byte[] ByteStream)
{
Guid id = Guid.Empty;
//upload
using (RepoDbWave dbc = new RepoDbWave())
{
FileItem f = new FileItem();
var count_row = dbc.FileItems.Count(a => a.ID != Guid.Empty);
f.FileContent = ByteStream;
f.FileSize = f.FileContent.Length;
f.Time = DateTime.Now;
FileItem newItem = dbc.FileItems.Add(f);
dbc.SaveChanges();
id = newItem.ID;
}
return id;
}
和我的请求代码:
private void btnUpload_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Wave files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
string WaveLocation = open.FileName;
txtUpload.Text = WaveLocation;
byte[] WavebyteArray = File.ReadAllBytes(WaveLocation);
///webClient//////////////////////////////////////////////
WebClient Proxy1 = new WebClient();
Proxy1.Headers["Content-type"] = "application/json";
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(byte[]));
serializerToUplaod.WriteObject(ms, WavebyteArray);
byte[] data = Proxy1.UploadData("http://localhost:1866/TransferService.svc/UploadFile", "POST", ms.ToArray());
MemoryStream stream = new MemoryStream(data);
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(byte[]));
var guID = obj.ReadObject(stream);
lblUpload.Text = guID.ToString();
//////////////////////////////////////////////////////////
}
答案 0 :(得分:0)
我认为问题在于您将内容类型设置为application / json。但是你在正文中传递一个字节数组。这可能会混淆WCF。而不是使用内容类型作为json尝试使用任何流。