【问题标题】:Large File Upload Using HttpHandler or HttpModule?大文件上传使用 HttpHandler 还是 HttpModule?
【发布时间】:2012-04-27 09:56:12
【问题描述】:

我有一个网络表单应用程序。它需要能够上传大文件(100MB)。我打算使用 httpHandler 和 httpModule 将文件拆分为 chunk

我也看过http://forums.asp.net/t/55127.aspx

但这是一篇非常古老的帖子,我在互联网上看到了一些使用 httpHandler 的示例。

例如http://silverlightfileupld.codeplex.com/

我不确定 httpModule 是否比 httpHandler 更好。

由于httpModule是针对整个应用的请求的,我只想把它应用到指定页面。

谁能清楚地解释一下httpHandler 用于大文件上传的缺点(如果有的话)? 如果你知道一个没有 flash/silverlight 的好例子,你能把链接贴在这里吗?谢谢

编辑:希望看到一些源代码示例。

【问题讨论】:

标签: asp.net file-upload request httphandler httpmodule


【解决方案1】:

为什么不试试plupload,它有很多功能和很多回退,这里是如何完成的。

这是 http 处理程序代码:

Imports System
Imports System.IO
Imports System.Web


Public Class upload : Implements IHttpHandler


    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim chunk As Integer = If(context.Request("chunk") IsNot Nothing, Integer.Parse(context.Request("chunk")), 0)
        Dim fileName As String = If(context.Request("name") IsNot Nothing, context.Request("name"), String.Empty)

        Dim fileUpload As HttpPostedFile = context.Request.Files(0)

        Dim uploadPath = context.Server.MapPath("~/uploads")
        Using fs = New FileStream(Path.Combine(uploadPath, fileName), If(chunk = 0, FileMode.Create, FileMode.Append))
            Dim buffer = New Byte(fileUpload.InputStream.Length - 1) {}
            fileUpload.InputStream.Read(buffer, 0, buffer.Length)

            fs.Write(buffer, 0, buffer.Length)
        End Using

        context.Response.ContentType = "text/plain"
        context.Response.Write("Success")
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

【讨论】:

  • 找不到读取到块的部分,似乎 plupload 不支持 HTML4,谢谢。
  • 它支持HTML4。来自API文档plupload.com/plupload/docs/api/…
  • 出于兴趣,为什么缓冲区设置为输入流长度减1?
猜你喜欢
  • 2020-06-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多