我已经看过很多解决方案,但没有人帮助解决此问题。发送流上传文件时,wcf流长度= 0
我有一个应该用于文件上传的wcf服务。
由于我想上传大文件,我没有使用WebHttpRequest,我已经添加了wcf的服务参考,我正在使用它。
这里的服务接口:
[ServiceContract(Namespace = "https://Services.XXX.com", ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
public interface IAttachmentService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "UploadFile", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string UploadFile(Stream file);
}
,这里是我如何发送该视频流:
using (MemoryStream stream = new MemoryStream())
{
//write some data to the the memory stream
stream.Write(len, 0, len.Length);
stream.Write(jsonFileBytes, 0, jsonFileBytes.Length);
//write the file data to the memory stream
while ((bytesReadCount = file.Read(bufferRead, 0, bufferRead.Length)) > 0)
{
stream.Write(bufferRead, 0, bytesReadCount);
}
stream.Position = 0;
byte[] array = stream.ToArray();
WCFClientProxy<Attachment.Interfaces.IAttachmentService> proxy = new WCFClientProxy<Attachment.Interfaces.IAttachmentService>();
return Serialization.ConvertToJson(new { IsError = false, Files = proxy.Instance.UploadFile(array) });
}
但接收,以保存该文件流的方法流的长度为0
我也尝试发送这样的字节[]:
using (MemoryStream stream = new MemoryStream())
{
//write some data to the the memory stream
stream.Write(len, 0, len.Length);
stream.Write(jsonFileBytes, 0, jsonFileBytes.Length);
//write the file data to the memory stream
while ((bytesReadCount = file.Read(bufferRead, 0, bufferRead.Length)) > 0)
{
stream.Write(bufferRead, 0, bytesReadCount);
}
stream.Position = 0;
byte[] array = stream.ToArray();
WCFClientProxy<Attachment.Interfaces.IAttachmentService> proxy = new WCFClientProxy<Attachment.Interfaces.IAttachmentService>();
return Serialization.ConvertToJson(new { IsError = false, Files = proxy.Instance.UploadFile(array) });
}
,但这种做法我得到这个错误:
The remote server returned an unexpected response: (400) Bad Request.
难道ÿ你在'处理一些代码'写完之后回滚流? –
你的意思是这样的:'stream.position = 0;'?如果是这样,比是的,我试过了。在我的'一些代码...'我加入了一些我需要的数据的文件流,因为当通过WCF传递流时,我无法传递任何omre参数 –
'stream.Position = 0'。 –