System.InvalidCastException: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'

I am trying to create a webpage that takes a file that someone want to upload and chop the file into smaller pieces, and then put back together again.  I have been trying to convert some code I found on Codeproject (FileSplitMerge).  They did it with VB which is what I want to use but it is a project not a web site so...  The problem that I am running into is I keep getting an error when I try to set:

FileStream = HttpPostedFile.InputStream

(I am using variables in my code) but I keep getting an error of:

System.InvalidCastException: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'

Does anyone know what I need to do to fix this? 

 

 My acctual code is below: 

Public File_Name As String

Public Chunk_Size As Integer = 5120

Public File_Size As Long

Public OutPut_Path As String

Public Errors As String

Public files As HttpPostedFile

Public File_Stream As String

 

Public Sub SplitFile()

'Declarations

Dim Index As Short

Dim OutputFile As String

Dim StartPosition As Long

Dim Buffer As Byte() = New Byte() {}

Dim InputFileStram As FileStream

Dim OutputFileStram As FileStream

Dim BinaryWriter As BinaryWriter

Dim BinaryReader As BinaryReader

Dim Fragments As Short

Dim RemainingBytes As Long

Dim SplitFileName As String

Try

Errors = ""

SplitFileName = File_Name

InputFileStram = files.InputStream '***************Problem is Here*********

BinaryReader = New BinaryReader(InputFileStram)

If Not File.Exists(SplitFileName) Then

Errors = "File " & File_Name & "doesn't exsist"

End If

If Errors = "" Then

If File_Size <= Chunk_Size Then

Chunk_Size = CInt(File_Size / 2)

End If

Fragments = Math.Floor(CDec(File_Size / Chunk_Size))

RemainingBytes = File_Size - (Fragments * Chunk_Size)

Directory.CreateDirectory(OutPut_Path)

BinaryReader.BaseStream.Seek(0, SeekOrigin.Begin)

For Index = 1 To Fragments

OutputFile = OutPut_Path & "/" & File_Name & "." & Format(Index, "000")

ReDim Buffer(Chunk_Size - 1)

BinaryReader.Read(Buffer, 0, Chunk_Size)

StartPosition = BinaryReader.BaseStream.Seek(0, SeekOrigin.Current)

If File.Exists(OutputFile) Then

File.Delete(OutputFile)

End If

OutputFileStram = New FileStream(OutputFile, FileMode.Create)BinaryWriter = New BinaryWriter(OutputFileStram)

BinaryWriter.Write(Buffer)

OutputFileStram.Flush()

BinaryWriter.Close()

OutputFileStram.Close()

Next

If RemainingBytes > 0 Then

OutputFile = OutPut_Path & "\" & File_Name & "." & Format(Index, "000")ReDim Buffer(RemainingBytes - 1)

BinaryReader.Read(Buffer, 0, RemainingBytes)

If File.Exists(OutputFile) Then

File.Delete(OutputFile)

End If

OutputFileStram = New FileStream(OutputFile, FileMode.Create)BinaryWriter = New BinaryWriter(OutputFileStram)

BinaryWriter.Write(Buffer)

OutputFileStram.Flush()

BinaryWriter.Close()

OutputFileStram.Close()

End If

InputFileStram.Close()

BinaryReader.Close()

Errors = ""

End If

Catch ex As Exception

Errors = ex.ToString

Finally

BinaryWriter = Nothing

OutputFileStram = Nothing

BinaryReader = Nothing

InputFileStram = Nothing

End Try

End Sub

0
jharwood
7/30/2007 8:39:35 PM
asp.net.visual-basic.net 3721 articles. 0 followers. Follow

2 Replies
3363 Views

Similar Articles

[PageSpeed] 54

Just Delclare

Dim InputFileStram As System.IO.Stream
 

DK

0
DkUltra
7/30/2007 9:31:26 PM

That got me past that problem thanks.

0
jharwood
7/31/2007 12:52:03 PM
Reply:

Web resources about - System.InvalidCastException: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream' - asp.net.visual-basic.net

Resources last updated: 2/8/2016 11:43:31 AM