• 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
  • 相关阅读:
    轻松构建微服务之分布式事物
    Java和操作系统交互细节
    网络内核之TCP是如何发送和接收消息的
    最全的微服务知识科普
    【译】TCP Implementation in Linux
    轻松构建微服务之分库分表
    OSX的一些基本知识
    Delphi中使用比较少的一些语法
    热烈祝贺我的博客开通
    Windows 服务快捷启动命令
  • 原文地址:https://www.cnblogs.com/cy163/p/229963.html
Copyright © 2020-2023  润新知