Is is possible to programmaticaly save a graphic forma given URL(文件下载,进度条)
http://www.webnewsgroups.net/group/microsoft.public.dotnet.languages_vb/topic8437.aspx
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
but the file still displays nothing.
Dim URL As String = GraphicURL
Dim request As WebRequest = WebRequest.Create(URL)
Dim response As WebResponse = request.GetResponse()
Dim reader As StreamReader = New
StreamReader(response.GetResponseStream())
Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
".jpg")
writer.Write(reader)
writer.Close()
reader.Close()
response.Close()
Stacey Levine
Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
want to save that graphic to a local file. The approach below gets the
response, but I can't quite figure out how to save it to a file. In this
instance, a file gets created but it only contains the text
"System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
but the file still displays nothing.
Dim URL As String = GraphicURL
Dim request As WebRequest = WebRequest.Create(URL)
Dim response As WebResponse = request.GetResponse()
Dim reader As StreamReader = New
StreamReader(response.GetResponseStream())
Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
".jpg")
writer.Write(reader)
writer.Close()
reader.Close()
response.Close()
Stacey Levine
Yes, you are writing the reader, rather than writing what the reader is
reading :) Try writing reader.readtoend (or something).
reading :) Try writing reader.readtoend (or something).
Show quote
"Stacey Levine" <stacey@newsgroup.nospam> wrote in message
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
I did try writer.write(reader.ReadToEnd), and the file has more data.. it
is all hex.. but it still won't open as a picture. I am not sure if I missed
writing something.
is all hex.. but it still won't open as a picture. I am not sure if I missed
writing something.
Show quote
"Robin Tucker" <idontwanttobespammedanymore@reallyidont.com> wrote in
message news:dajr8p$la7$1$830fa79d@news.demon.co.uk...
> Yes, you are writing the reader, rather than writing what the reader is
> reading :) Try writing reader.readtoend (or something).
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
message news:dajr8p$la7$1$830fa79d@news.demon.co.uk...
> Yes, you are writing the reader, rather than writing what the reader is
> reading :) Try writing reader.readtoend (or something).
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
One other wierd bit.. when I save the file using the reader.ReadToEnd ..
the file has 1302 bytes.
If I right click the image and save it, it has 2257 bytes.
very very wierd
the file has 1302 bytes.
If I right click the image and save it, it has 2257 bytes.
very very wierd
Show quote
"Stacey Levine" <stacey@newsgroup.nospam> wrote in message
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
You're using a regular StreamReader and StreamWriter, so it's using Default
Text encodings. You need to use BinaryReader/BinaryWriter for Binary
graphic data. See other post.
Text encodings. You need to use BinaryReader/BinaryWriter for Binary
graphic data. See other post.
Show quote
"Stacey Levine" <stacey@newsgroup.nospam> wrote in message
news:evkHGFygFHA.2424@TK2MSFTNGP09.phx.gbl...
> One other wierd bit.. when I save the file using the reader.ReadToEnd ..
> the file has 1302 bytes.
> If I right click the image and save it, it has 2257 bytes.
>
> very very wierd
>
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
news:evkHGFygFHA.2424@TK2MSFTNGP09.phx.gbl...
> One other wierd bit.. when I save the file using the reader.ReadToEnd ..
> the file has 1302 bytes.
> If I right click the image and save it, it has 2257 bytes.
>
> very very wierd
>
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
The data returned is probably gzip'ed(documented in RFC1950 to RFC 1952),
you dont want to read the HTTP data as GZIP if you dont intend to unzip it
programatically.
you dont want to read the HTTP data as GZIP if you dont intend to unzip it
programatically.
Show quote
"Stacey Levine" <stacey@newsgroup.nospam> skrev i melding
news:evkHGFygFHA.2424@TK2MSFTNGP09.phx.gbl...
> One other wierd bit.. when I save the file using the reader.ReadToEnd ..
> the file has 1302 bytes.
> If I right click the image and save it, it has 2257 bytes.
>
> very very wierd
>
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
news:evkHGFygFHA.2424@TK2MSFTNGP09.phx.gbl...
> One other wierd bit.. when I save the file using the reader.ReadToEnd ..
> the file has 1302 bytes.
> If I right click the image and save it, it has 2257 bytes.
>
> very very wierd
>
>
>
> "Stacey Levine" <stacey@newsgroup.nospam> wrote in message
> news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
>> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
>> want to save that graphic to a local file. The approach below gets the
>> response, but I can't quite figure out how to save it to a file. In this
>> instance, a file gets created but it only contains the text
>> "System.Io.StreamReader" I have also tried
>> writer.write(reader.ReadToEnd), but the file still displays nothing.
>>
>>
>> Dim URL As String = GraphicURL
>>
>> Dim request As WebRequest = WebRequest.Create(URL)
>>
>> Dim response As WebResponse = request.GetResponse()
>>
>> Dim reader As StreamReader = New
>>
>> StreamReader(response.GetResponseStream())
>>
>> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
>> ".jpg")
>>
>> writer.Write(reader)
>>
>> writer.Close()
>>
>> reader.Close()
>>
>> response.Close()
>>
>>
>> Stacey Levine
>>
>>
>
>
Try BinaryWriter/BinaryReader:
Try
Dim Request As HttpWebRequest =
HttpWebRequest.Create(http://i3.microsoft.com/h/en-us/i/SpeedPC_2_10.jpg)
Request.Timeout = 5000
Dim Response As HttpWebResponse = Request.GetResponse()
Dim myReader As BinaryReader
myReader = New BinaryReader(Response.GetResponseStream())
Dim Result() As Byte = myReader.ReadBytes(Response.ContentLength)
Dim myFileStream As New FileStream("c:\SpeedPC_2_10.jpg",
FileMode.CreateNew)
Dim myWriter As New BinaryWriter(myFileStream)
myWriter.Write(Result)
myWriter.Close()
Catch e As Exception
MessageBox.Show(e.Message, "Error Occurred")
End Try
Try
Dim Request As HttpWebRequest =
HttpWebRequest.Create(http://i3.microsoft.com/h/en-us/i/SpeedPC_2_10.jpg)
Request.Timeout = 5000
Dim Response As HttpWebResponse = Request.GetResponse()
Dim myReader As BinaryReader
myReader = New BinaryReader(Response.GetResponseStream())
Dim Result() As Byte = myReader.ReadBytes(Response.ContentLength)
Dim myFileStream As New FileStream("c:\SpeedPC_2_10.jpg",
FileMode.CreateNew)
Dim myWriter As New BinaryWriter(myFileStream)
myWriter.Write(Result)
myWriter.Close()
Catch e As Exception
MessageBox.Show(e.Message, "Error Occurred")
End Try
Show quote
"Stacey Levine" <stacey@newsgroup.nospam> wrote in message
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
news:%23E95twxgFHA.2896@TK2MSFTNGP09.phx.gbl...
> Ok.. Maybe I am trying the wrong approach. If given a URL to a graphic, I
> want to save that graphic to a local file. The approach below gets the
> response, but I can't quite figure out how to save it to a file. In this
> instance, a file gets created but it only contains the text
> "System.Io.StreamReader" I have also tried writer.write(reader.ReadToEnd),
> but the file still displays nothing.
>
>
> Dim URL As String = GraphicURL
>
> Dim request As WebRequest = WebRequest.Create(URL)
>
> Dim response As WebResponse = request.GetResponse()
>
> Dim reader As StreamReader = New
>
> StreamReader(response.GetResponseStream())
>
> Dim writer As StreamWriter = New StreamWriter("C:\temp\" & GraphicKey &
> ".jpg")
>
> writer.Write(reader)
>
> writer.Close()
>
> reader.Close()
>
> response.Close()
>
>
> Stacey Levine
>
>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步