我一直在尝试将文件流式传输到我的Web服务。在我的控制器(ApiController)我有一个帖子功能如下:Asp.net Web Api流式传输
public void Post(Stream stream)
{
if (stream != null && stream.Length > 0)
{
_websitesContext.Files.Add(new DbFile() { Filename = Guid.NewGuid().ToString(), FileBytes= ToBytes(stream) });
_websitesContext.SaveChanges();
}
}
我一直在试图流的文件与我的Web客户端通过执行以下操作:
public void UploadFileStream(HttpPostedFileBase file)
{
WebClient myWebClient = new WebClient();
Stream postStream = myWebClient.OpenWrite(GetFileServiceUrl(), "POST");
var buffer = ToBytes(file.InputStream);
postStream.Write(buffer, 0,buffer.Length);
postStream.Close();
}
现在,当我调试我Web服务,它进入Post函数,但流始终为空。想知道是否有人可能有一个想法,为什么发生这种情况?
什么是_websitesContext,你如何获得一个流作为动作参数?可能您应该访问请求和响应流 –
_websitesContext用于将条目添加到数据库中。 Parv是不可能有一个流作为一个动作参数? – TBD
另外,据我所知(但我可能会误解,或者在这里错误地记住),这在ASP.NET中不容易实现,导致ASP.NET等待,直到整个请求上传,直到它调用任何方法/文件位于该终点。 – Alxandr