Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'

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

Scheduled Pinned Locked Moved ASP.NET
sysadminhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silentspeaker
    wrote on last edited by
    #1

    Hi,
    I want to upload browsed file to the ftp server but getting this error

    Sub UploadFile()

        Try
            Dim fileName As String = FulFile.PostedFile.FileName
            Dim requestFTP As WebRequest = WebRequest.Create("ftp://...................." & fileName)
            requestFTP.Credentials = New NetworkCredential("userid", "password")
            requestFTP.Method = WebRequestMethods.Ftp.UploadFile
    
    
            Dim fStream As FileStream = FulFile.PostedFile.InputStream 'Error:Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'.
    
    
            Dim bufferLength As Integer = 2048
            Dim buffer As Byte() = New Byte(bufferLength - 1) {}
    
            Dim uploadStream As Stream = requestFTP.GetRequestStream()
            Dim contentLength As Integer = fStream.Read(buffer, 0, bufferLength)
    
            While contentLength <> 0
                uploadStream.Write(buffer, 0, contentLength)
                contentLength = fStream.Read(buffer, 0, bufferLength)
            End While
            uploadStream.Close()
            fStream.Close()
            requestFTP = Nothing
    
            LblMsg.Text = "File Uploading Is SuccessFull..."
    
        Catch ep As Exception
            LblMsg.Text = ep.Message
        End Try
    
    End Sub
    
    Richard DeemingR 1 Reply Last reply
    0
    • S silentspeaker

      Hi,
      I want to upload browsed file to the ftp server but getting this error

      Sub UploadFile()

          Try
              Dim fileName As String = FulFile.PostedFile.FileName
              Dim requestFTP As WebRequest = WebRequest.Create("ftp://...................." & fileName)
              requestFTP.Credentials = New NetworkCredential("userid", "password")
              requestFTP.Method = WebRequestMethods.Ftp.UploadFile
      
      
              Dim fStream As FileStream = FulFile.PostedFile.InputStream 'Error:Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'.
      
      
              Dim bufferLength As Integer = 2048
              Dim buffer As Byte() = New Byte(bufferLength - 1) {}
      
              Dim uploadStream As Stream = requestFTP.GetRequestStream()
              Dim contentLength As Integer = fStream.Read(buffer, 0, bufferLength)
      
              While contentLength <> 0
                  uploadStream.Write(buffer, 0, contentLength)
                  contentLength = fStream.Read(buffer, 0, bufferLength)
              End While
              uploadStream.Close()
              fStream.Close()
              requestFTP = Nothing
      
              LblMsg.Text = "File Uploading Is SuccessFull..."
      
          Catch ep As Exception
              LblMsg.Text = ep.Message
          End Try
      
      End Sub
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      I'm not sure how the error message could be any clearer:

      • the HttpPostedFile.InputStream property is a System.Web.HttpInputStream, which inherits from System.IO.Stream;
      • you are attempting to store it in a variable of type System.IO.FileStream, which is a different type of System.IO.Stream.

      Change your fStream variable to be a System.IO.Stream:

      Dim fStream As Stream = FulFile.PostedFile.InputStream

      Also, you're still not calling the GetResponse method on your requestFTP object. If you don't call that method, your request will never be sent.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        I'm not sure how the error message could be any clearer:

        • the HttpPostedFile.InputStream property is a System.Web.HttpInputStream, which inherits from System.IO.Stream;
        • you are attempting to store it in a variable of type System.IO.FileStream, which is a different type of System.IO.Stream.

        Change your fStream variable to be a System.IO.Stream:

        Dim fStream As Stream = FulFile.PostedFile.InputStream

        Also, you're still not calling the GetResponse method on your requestFTP object. If you don't call that method, your request will never be sent.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        S Offline
        S Offline
        silentspeaker
        wrote on last edited by
        #3

        GetResponse and requestFTP what are these and how can we upload using GetResponse and requestFTP bcoz it showing that

        Network Error (tcp_error)

        A communication error occurred: ""
        The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.

        Richard DeemingR 1 Reply Last reply
        0
        • S silentspeaker

          GetResponse and requestFTP what are these and how can we upload using GetResponse and requestFTP bcoz it showing that

          Network Error (tcp_error)

          A communication error occurred: ""
          The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          requestFTP is a variable in the code you just posted. GetResponse is a method on the FtpWebRequest type. The error you keep posting to different threads is most likely related to a timeout or firewall issue, and is nothing to do with the question you asked in the first post of this thread.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups