473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Saving a stream to a file

I've downloaded a file from an FTP server and
using WebRequestMethods.Ftp.DownloadFile and the
result is, i guess, a file. However, the response is
of type FtpWebResponse and as such only have a method
for obtaining a Stream object only...

I'd like it to respond with an object of type File or
FileInfo, maybe, directly. Is it doable? Or is the
best way to do that simply using FileWriter?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 27 '08 #1
8 1083
K Viltersten wrote:
I've downloaded a file from an FTP server and
using WebRequestMethods.Ftp.DownloadFile and the
result is, i guess, a file. However, the response is
of type FtpWebResponse and as such only have a method
for obtaining a Stream object only...

I'd like it to respond with an object of type File or
FileInfo, maybe, directly. Is it doable?
It has the methods it ha.

WebClient has some convenience methods like DownloadFile.

Arne
Jul 27 '08 #2
>I've downloaded a file from an FTP server and
>using WebRequestMethods.Ftp.DownloadFile and the
result is, i guess, a file. However, the response is
of type FtpWebResponse and as such only have a method
for obtaining a Stream object only...

I'd like it to respond with an object of type File or
FileInfo, maybe, directly. Is it doable?

It has the methods it ha.

WebClient has some convenience methods like DownloadFile.
Right, thanks! I was hoping for a neat one-liner
doing everything automagically for me. :)

Perhaps i'm getting too convenient, hehe.
--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 27 '08 #3
Hello K,

Here ya go:

Public Function Download(ByVal RemoteFile As String, ByVal LocalFile
As String) As Boolean
Try
Dim ftp As FtpWebRequest = GetRequest(GetURI(RemoteFile))
ftp.Method = WebRequestMethods.Ftp.DownloadFile

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse,
System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
Using fs As New IO.FileStream(LocalFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using

Catch ex As Exception
_error = ex
Return False
End Try
Return True
End Function

I've downloaded a file from an FTP server and
using WebRequestMethods.Ftp.DownloadFile and the
result is, i guess, a file. However, the response is
of type FtpWebResponse and as such only have a method
for obtaining a Stream object only...
I'd like it to respond with an object of type File or FileInfo, maybe,
directly. Is it doable? Or is the best way to do that simply using
FileWriter?

Jul 31 '08 #4
On Thu, 31 Jul 2008 14:01:01 -0700, The Colorado Kid <tckwrote:
Hello K,

Here ya go: [VB code snipped]
Uh...I don't mean to be a bother, but traditionally here in the C#
newsgroup, we try to stick to posting C# code. :)
Jul 31 '08 #5
Hello Peter,

Aha! Helps if I pay attention to which newsgroup I'm pulling articles from.
Sorry about that. I've got a slew of newsgroups that all come up in the same
stream. I'll pay closer attention in the future.

Ironically, I had to convert that code from C# to VB for the app it's used
in...
On Thu, 31 Jul 2008 14:01:01 -0700, The Colorado Kid <tckwrote:
>Hello K,

Here ya go: [VB code snipped]
Uh...I don't mean to be a bother, but traditionally here in the C#
newsgroup, we try to stick to posting C# code. :)

Aug 1 '08 #6
Ironically, I had to convert that code from C# to VB for the app it's used
in...
How about reposting it in the
before-conversion state, then?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Aug 1 '08 #7
On Fri, 01 Aug 2008 00:12:16 -0700, The Colorado Kid <tckwrote:
Hello Peter,

Aha! Helps if I pay attention to which newsgroup I'm pulling articles
from. Sorry about that. I've got a slew of newsgroups that all come up
in the same stream. I'll pay closer attention in the future.
It's not a big deal. I just thought it was funny. Heck, I'm the one who
answered a C# question here as if it were about Java just the other day.
I know how hard it can be to keep your newsgroups straight.
Ironically, I had to convert that code from C# to VB for the app it's
used in...
Yes, that is ironic. :) As Konrad says, maybe it would help to post the
original C# then?

Pete
Aug 1 '08 #8
Peter,

I can't find the original source, but here's something similar:

http://www.codeproject.com/KB/IP/Ftp...w=Quick&fr=201
On Fri, 01 Aug 2008 00:12:16 -0700, The Colorado Kid <tckwrote:
>Hello Peter,

Aha! Helps if I pay attention to which newsgroup I'm pulling articles
from. Sorry about that. I've got a slew of newsgroups that all come
up in the same stream. I'll pay closer attention in the future.
It's not a big deal. I just thought it was funny. Heck, I'm the one
who answered a C# question here as if it were about Java just the
other day. I know how hard it can be to keep your newsgroups
straight.
>Ironically, I had to convert that code from C# to VB for the app it's
used in...
Yes, that is ironic. :) As Konrad says, maybe it would help to post
the original C# then?

Pete

Aug 1 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Umesh | last post by:
Hi, I have an Application, in which 1) need to post data to a URL(Remote Server), by using HTTPRequest. 2) get the Image data in the form of Stream in Response. 3) need to save this stream as a...
0
by: Umesh | last post by:
Hi Gurus, I have an Application, in which 1) need to post data to a URL(Remote Server), by using HTTPRequest. 2) get the Image data in the form of Stream in Response. 3) need to save this...
0
by: Umesh | last post by:
Hi, I have an Application, in which 1) need to post data to a URL(Remote Server), by using HTTPRequest. 2) get the Image data in the form of Stream in Response. 3) need to save this stream as a...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
6
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.