zoukankan      html  css  js  c++  java
  • 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

    Author
    7 Jul 2005 6:11 PM
    Robin Tucker
    Yes, you are writing the reader, rather than writing what the reader is
    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
    >
    >
    Author
    7 Jul 2005 6:18 PM
    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.
    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
    >>
    >>
    >
    >
    Author
    7 Jul 2005 6:26 PM
    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



    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
    >
    >
    Author
    7 Jul 2005 6:50 PM
    Michael C#
    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.

    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
    >>
    >>
    >
    >
    Author
    7 Jul 2005 10:03 PM
    Inge Henriksen
    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.


    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
    >>
    >>
    >
    >
    Author
    7 Jul 2005 6:49 PM
    Michael C#
    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


    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
    >
    >
    Author
    7 Jul 2005 7:17 PM
    Stacey Levine
    That was it. Thanks. I was banging my head against the wall.

    Stacey
  • 相关阅读:
    Kafka Replication: The case for MirrorMaker 2.0
    CentOs7.3 搭建 SolrCloud 集群服务
    Redis 集群:CLUSTERDOWN The cluster is down
    Redis Cluster: (error) MOVED
    Kafka重启出错:Corrupt index found
    redis cluster slots数量 为何是16384(2的14次方)
    redis监控工具汇总
    Redis运维利器 -- RedisManager
    redis三种集群策略
    Deploy custom service on non hadoop node with Apache Ambari
  • 原文地址:https://www.cnblogs.com/cy163/p/229963.html
Copyright © 2011-2022 走看看