This is a migrated thread and some comments may be shown as answers.

RadAsyncUpload with custom handler fails for small files

1 Answer 81 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
M B
Top achievements
Rank 1
M B asked on 28 Nov 2012, 06:56 PM
I am using RadAsyncUpload with a custom handler based on the demo code. The handler works fine with larger files that take more than a couple seconds to upload. When uploading smaller files that upload almost instantly I get the following error in the custom handler:
Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'.

Protected Overrides Function Process(ByVal myFile As Telerik.Web.UI.UploadedFile, ByVal context As HttpContext, ByVal configuration As IAsyncUploadConfiguration, ByVal tempFileName As String) As IAsyncUploadResult
 
    Dim result As MyAsyncUploadResult = CreateDefaultUploadResult(Of MyAsyncUploadResult)(myFile)
    Try
 
      Dim userID As String = ""
 
      Dim myConfiguration As MyAsyncUploadConfiguration = TryCast(configuration, MyAsyncUploadConfiguration)
      If myConfiguration IsNot Nothing Then
        userID = myConfiguration.UserID
      End If
 
      result.FileID = MySaveFile(myFile, userID)
 
    Catch ex As Exception
      Dim sRtn As String = SendErrorEmail(ex, "", "", "handler.ashx", "", "", "")
    End Try
 
    Return result
  End Function
 
  Public Function MySaveFile(ByVal upFile As Telerik.Web.UI.UploadedFile, ByVal userID As String) As String
    Dim sOutputEncrypt As String = ""
    Dim destinationPath As String = "C:\sitefolder\App_Data\Files\"
    Try
      Dim key As Byte() = CreateKey("password")
      Dim IV As Byte() = CreateIV("password")
      Dim sFileName As String = upFile.FileName
      sOutputEncrypt = destinationPath & upFile.GetName.Replace(".", "_") & ".encrypt"
 
      Using fStream As FileStream = upFile.InputStream 'File.Open(filename, FileMode.OpenOrCreate)
        Dim RijndaelAlg As Rijndael = Rijndael.Create()
        Using cStream As New CryptoStream(fStream, RijndaelAlg.CreateEncryptor(key, IV), CryptoStreamMode.Read)
          Using destination As FileStream = File.Create(sOutputEncrypt)
            cStream.CopyTo(destination)
          End Using
        End Using
      End Using
 
     Catch ex As Exception
      Dim sRtn As String = SendErrorEmail(ex, "", "", "handler.ashx", "", "", "")
    End Try
    Return sOutputEncrypt
  End Function

1 Answer, 1 is accepted

Sort by
0
M B
Top achievements
Rank 1
answered on 29 Nov 2012, 01:08 AM
Figured out my problem.
Using fStream As FileStream = upFile.InputStream
 
needs to be
 
Using fStream As System.IO.Stream = upFile.InputStream

Not sure why the original code worked for large files but not small ones.
Tags
AsyncUpload
Asked by
M B
Top achievements
Rank 1
Answers by
M B
Top achievements
Rank 1
Share this question
or