简体   繁体   English

ASP.NET MVC 如何在完全上传之前开始读取已发布的文件流?

[英]ASP.NET MVC How can I begin reading from a posted file stream before it is fully uploaded?

I have an ASP.NET MVC action that receives a file via HttpPostedFileBase.我有一个通过 HttpPostedFileBase 接收文件的 ASP.NET MVC 操作。

I want to start reading from the stream as soon as possible.我想尽快开始从流中读取。 This is so I can report upload progress (via a separate mechanism).这样我就可以报告上传进度(通过单独的机制)。 The issue that I have is that my break point inside this action is not hit until the uploaded file is available in it's entirety.我遇到的问题是,在上传的文件完整可用之前,我在此操作中的断点不会被击中。

How can I being reading the posted file stream before it's fully uploaded?如何在完全上传之前阅读已发布的文件流?

[HttpPost]
public ActionResult Upload(HttpPostedFileBase uploadFile)
{
    ... uploadFile has been fully uploaded before this point
}

Edit: Update - I've now tried another way, implementing this logic via an HttpHandler, but still, the moment I begin to read from the stream, the code blocks until the file stream has completed upload to the server.编辑:更新 - 我现在尝试了另一种方式,通过 HttpHandler 实现这个逻辑,但是,当我开始从流中读取时,代码会阻塞,直到文件流完成上传到服务器。

No luck as yet.目前还没有运气。

The problem is that using several properties on the HttpContext.Request object cause ASP.NET to wait until the full post data has been processed.问题是在 HttpContext.Request 对象上使用多个属性会导致 ASP.NET 等待,直到处理完完整的发布数据。 The key to circumventing this issue is HttpContext.Request.GetBufferlessInputStream().绕过这个问题的关键是 HttpContext.Request.GetBufferlessInputStream()。 This however requires processing, and there is no built in way to do this.然而,这需要处理,并且没有内置的方法来做到这一点。

The solution in the end was to create classes that can parse the multipart content in the raw post data, which I achieved by looking at how its done in ASP.NETs HttpContext.Request code.最终的解决方案是创建可以解析原始帖子数据中的多部分内容的类,我通过查看它在 ASP.NETs HttpContext.Request 代码中是如何完成的来实现的。 I found various components in System.Web and was able to adapt to my needs.我在 System.Web 中找到了各种组件,并且能够适应我的需求。

The code now makes up part of the project I've written up here:代码现在构成了我在这里编写的项目的一部分:

https://journals.appsoftware.com/public/20/97/3819/programming-blog/development/asp.net-file-uploader-with-signalr-progress-bar-and-extended-input-stream-processing https://journals.appsoftware.com/public/20/97/3819/programming-blog/development/asp.net-file-uploader-with-signalr-progress-bar-and-extended-input-stream-processing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM