Hi. I have a requirement to upload large pdf files from a customer and after some thought decided to see if I can upload the file in chunks to hopefully lighten the load on the server a bit. I have the below code but it's not working properly. The resulting file is way too large and when I try to open it, it will not open. I receive an error that it is corrupted. I start with a file that is 20 MB and end up with a file that is closer to 1 GB. I wanted to look at what is coming in the request to see what all it's writing but have no idea how to or if I even can somehow display it.
I just read on another post that .NET is not capable of streaming files to the hard disk but that was from 2005. Is this still the case? This application is written in framework 2.0.
Code is below:
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ChunkData
Public Class MyModule
Implements IHttpModule
Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
AddHandler app.BeginRequest, New EventHandler(AddressOf app_BeginRequest)
End Sub
Private Sub app_BeginRequest(sender As Object, e As EventArgs)
Dim context As HttpContext = DirectCast(sender, HttpApplication).Context
If context.Request.ContentLength > 204800 Then
Dim provider As IServiceProvider = DirectCast(context, IServiceProvider)
Dim wr As HttpWorkerRequest = DirectCast(provider.GetService(GetType(HttpWorkerRequest)), HttpWorkerRequest)
Dim fs As FileStream = Nothing
' Check if body contains data
If wr.HasEntityBody() Then
' get the total body length
Dim requestLength As Integer = wr.GetTotalEntityBodyLength()
' Get the initial bytes loaded
Dim initialBytes As Integer = wr.GetPreloadedEntityBodyLength
If Not wr.IsEntireEntityBodyIsPreloaded() Then
Dim buffer As Byte() = New Byte(409600) {}
fs = New FileStream(context.Server.MapPath("~/Uploads/testdoc.pdf", FileMode.CreateNew)
' Set the received bytes to initial bytes before start reading
Dim receivedBytes As Integer = initialBytes
While requestLength - receivedBytes >= initialBytes
' Read another set of bytes
initialBytes = wr.ReadEntityBody(buffer, buffer.Length)
' Write the chunks to the physical file
fs.Write(buffer, 0, buffer.Length)
' Update the received bytes
receivedBytes += initialBytes
End While
initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes)
End If
End If
fs.Flush()
fs.Close()
End If
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
End Class
End Namespace
I just read on another post that .NET is not capable of streaming files to the hard disk but that was from 2005. Is this still the case? This application is written in framework 2.0.
Code is below:
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ChunkData
Public Class MyModule
Implements IHttpModule
Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
AddHandler app.BeginRequest, New EventHandler(AddressOf app_BeginRequest)
End Sub
Private Sub app_BeginRequest(sender As Object, e As EventArgs)
Dim context As HttpContext = DirectCast(sender, HttpApplication).Context
If context.Request.ContentLength > 204800 Then
Dim provider As IServiceProvider = DirectCast(context, IServiceProvider)
Dim wr As HttpWorkerRequest = DirectCast(provider.GetService(GetType(HttpWorkerRequest)), HttpWorkerRequest)
Dim fs As FileStream = Nothing
' Check if body contains data
If wr.HasEntityBody() Then
' get the total body length
Dim requestLength As Integer = wr.GetTotalEntityBodyLength()
' Get the initial bytes loaded
Dim initialBytes As Integer = wr.GetPreloadedEntityBodyLength
If Not wr.IsEntireEntityBodyIsPreloaded() Then
Dim buffer As Byte() = New Byte(409600) {}
fs = New FileStream(context.Server.MapPath("~/Uploads/testdoc.pdf", FileMode.CreateNew)
' Set the received bytes to initial bytes before start reading
Dim receivedBytes As Integer = initialBytes
While requestLength - receivedBytes >= initialBytes
' Read another set of bytes
initialBytes = wr.ReadEntityBody(buffer, buffer.Length)
' Write the chunks to the physical file
fs.Write(buffer, 0, buffer.Length)
' Update the received bytes
receivedBytes += initialBytes
End While
initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes)
End If
End If
fs.Flush()
fs.Close()
End If
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
End Class
End Namespace