有什么办法可以“偷看”吗?当文件通过 HTTP 上传到 Windows 机器时?

发布于 2024-08-25 18:51:48 字数 209 浏览 3 评论 0原文

我需要向 ASP.NET 网站添加文件上传功能,并且希望能够在上传时读取服务器上文件的一小部分。窥视或预览类型功能,以便我可以确定内容并在上传时向用户提供一些反馈(我们在这里讨论大文件)。有什么办法可以做到这一点吗?我正在考虑编写一个自定义控件的最坏情况,该控件在选择后仅上传固定数量的文件字节,然后在幕后开始另一个完整文件的上传。不完全确定这是否可能,但我正在寻找一个更优雅的解决方案......谢谢!

I need to add a file upload function to an ASP.NET website and would like to be able to read a small portion of the file on the server while it's still uploading. A peek or preview type function so I can determine contents and give some feedback to the user while it is still uploading (we're talking about large files here). Is there any way to do this? I'm thinking worst case of writing a custom control which uploads only a fixed number of bytes of the file once chosen and then under the covers starts another upload of the full file. Not totally sure even this is possible, but I'm looking for a more elegant solution anyway... Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血 2024-09-01 18:51:48

听起来您想在大文件上传期间避免“白屏死亡”。如果是这样,您可能需要研究 Telerik 的 RadUpload 控件 ,它上传过程中提供进度条。

如果你想推出自己的版本,我会反编译他们的试用版以获取想法。我以这种方式查看了他们的源代码,他们通过自定义 HttpModule 和 HttpHandler 及其控件的组合来完成进度条。处理程序以流式传输方式路由文件,而模块则提供“完成百分比”信息,反之亦然;我已经好几年没有看过它了。

编辑:

实际上,我正在尝试进行服务器端处理,因为文件仍在上传。我想通过 HTTP 导入用户数据,但想向用户展示我们在文件仍在上传时如何处理其数据的预览/选项(列定义等)。无论如何,我们都会按原样获取文件,因此上传不需要中断。鉴于我实际上希望在上传期间基于读取上传文件的相对较小部分进行交互,您仍然会推荐相同的方法吗?

嗯……这很难做到,而且可能无法跨浏览器工作,但可以用这种方法来完成。

正如我提到的,由于完全可以将传入文件作为流处理,因此您可以让初始处理更新某些状态作为流处理的一部分。如果您不作为流处理,则必须等待完整文件上传,然后才能对其执行任何操作。

问题是这样的:在文件上传过程中,你不能再进行基于HTML的交互。帖子必须继续保持不变,否则上传将失败。我链接的控件只能起作用,因为大多数浏览器允许 javascript 在发布期间继续执行和更新页面 DOM。

因此,为了完成这项工作,您必须在 HttpModule 中的文件处理过程中更新服务器端的一些标准化状态,这些状态通过 HttpHandler 处理的 XmlHttpRequest 调用传输回客户端。您必须使用纯 javascript/DOM 来更新用户的 UI。

因此,正如我所说,它很复杂,并且可能存在跨浏览器错误,但理论上是可以完成的。

有一些替代方案可能更稳定,但不一定可行:您可以构建一个 ActiveX 控件或 Click-Once .NET 应用程序,在上传之前预处理文件,甚至可以在用户继续时异步传输文件浏览。有些用户可能不喜欢这个选项,我不知道您的部署方案的详细信息,但它是一个选项。

It sounds like you want to avoid the "white screen of death" during large file uploads. If so, you might want to look into Telerik's RadUpload control , which provides a progress bar during upload.

If you want to roll your own, I'd decompile their trial copy for ideas. I've peeked at their source in this way, and they accomplish the progress bar through a combination of a custom HttpModule and HttpHandler along with their control. The handler routes the file in a streamed fashion while the module provides "percent complete" information--or the other way round; it's been a few years since I looked at it.

Edit:

Actually, I'm trying to do server-side processing as the file is still being uploaded. I want to import user data via HTTP, but want to present the user with preview/options of how we'll process their data while the file is still uploading (column definitions, etc.). No matter what, we'll take the file as is, so the upload doesn't need to be interrupted. Given that I actually want interaction during the upload based on reading a relatively small portion of the file as it is being uploaded, would you still recommend the same approach?

Well... it'd be very difficult to do, and it might not work cross-browser, but it could be done with this approach.

Since it's entirely possible to work with the incoming file as a stream as I mentioned, you could have your intial processing update some state as part of that stream processing. If you don't process as a stream, you have to wait for the full file upload before you can do anything with it.

The problem is this: during the file upload, you cannot have any more HTML-based interaction. The post must continue unabated or the upload will fail. The control I linked only works at all because most browsers allow javascript to continue to execute and update page DOM during the post.

So in order to make this work, you have to update some standardized state server-side during your file processing in the HttpModule, which is transmitted back to the client via XmlHttpRequest calls handled by the HttpHandler. You have to use pure javascript/DOM to update the UI for the user.

So, as I said, it's complex and likely to be buggy cross-browser, but it could theoretically be done.

There are alernatives that might be more stable, but might not necessarily be feasible: you could build an ActiveX control or a Click-Once .NET application that pre-processes the file before upload, and maybe even asynchronously transfers the file while the user continues browsing. Some users may not like that option, and I don't know the particulars of your deployment scenario, but it's an option.

維他命╮ 2024-09-01 18:51:48

HTTP HEAD 方法,但没有 PEEK

HEAD 将为您提供有关该文件的信息和标题。

当然,您可以创建一个特殊的请求处理程序来执行您想要的任何操作。您不必使用静态资源,您可以动态创建您想要的任何响应。

There is an HTTP HEAD method but not PEEK.

HEAD will give you information and headers about the file.

Of course you can make a special request handler that does anything you want. You don't have to work with static resources, you can dynamically create any response you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文