ASP.NET Upload Handler
Written as an IHttpHandler to hook into our own CRM system.
ASP.NET 3.5
HTML
<%@ WebHandler Language="VB" Class="Uploader" %>
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Web
Imports System.Web.SessionState
Public Class Uploader : Implements IHttpHandler, IReadOnlySessionState
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If context.Request.ContentLength > 4 * 1024 * 1024 Then
context.Response.StatusCode = 413
context.Response.StatusDescription = "Request Entity Too Large"
context.Response.ContentType = "text/plain"
context.Response.Write(String.Format("File is {0:N0} bytes long and larger than the {1:N0} bytes allowed by upload server", context.Request.ContentLength, 4 * 1024 * 1024))
context.Response.TrySkipIisCustomErrors = True
context.Response.Flush()
Exit Sub
End If
Dim fileName As String = context.Request.Headers("X-File-Name")
Dim fileType As String = context.Request.Headers("X-File-Type")
Dim fileSize As String = context.Request.Headers("X-File-Size")
If String.IsNullOrEmpty(fileName) Then
context.Response.StatusCode = 400
context.Response.StatusDescription = "Bad Request"
context.Response.ContentType = "text/plain"
context.Response.Write("Filename is missing")
context.Response.TrySkipIisCustomErrors = True
context.Response.Flush()
Exit Sub
End If
Dim filePath As String = context.Request.MapPath("~/files/")
If Not Directory.Exists(filePath) Then Directory.CreateDirectory(filePath)
filePath = Path.Combine(filePath, fileName)
Using fs As New FileStream(filePath, FileMode.OpenOrCreate)
context.Request.InputStream.CopyTo(fs)
End Using
'Save to database
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
...