Click here to Skip to main content
       

ASP.NET

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUnable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'membersilentspeaker8 Nov '12 - 1:55 
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

AnswerRe: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'memberRichard Deeming8 Nov '12 - 2:26 
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


GeneralRe: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'membersilentspeaker8 Nov '12 - 2:34 
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.

GeneralRe: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'memberRichard Deeming8 Nov '12 - 2:44 
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


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid