Download PDF using WebClient

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
bpi
Reply | Threaded
Open this post in threaded view
|

Download PDF using WebClient

bpi
Dear Squeakers,

I have a problem downloading a PDF file using WebClient. I use the following code:

        | client resp file |
        client := WebClient new.
        [resp := client httpGet: 'http://dl.dropbox.com/u/6204244/mcs-ftl.pdf'.
        resp isSuccess ifFalse:[^self error: resp status].
        "Stream the content from the response"
        file := FileStream newFileNamed: 'mcs-ftl.pdf'.
        resp streamTo: file size: resp contentLength progress: nil.
        file close] ensure: [client close]

I took the code from the WebClient Responses help page and only slightly modified it.

The problem is that I can open the file with Preview but all the pages are blank. The downloaded file has 9.9 MB while the original has 7.2 MB. So clearly something went wrong. Maybe some problem with the encoding?

Any idea how to fix that? I'd be most thankful.

Cheers,
Bernhard
Reply | Threaded
Open this post in threaded view
|

Re: Download PDF using WebClient

Levente Uzonyi-2
On Mon, 25 Oct 2010, Bernhard Pieber wrote:

> Dear Squeakers,
>
> I have a problem downloading a PDF file using WebClient. I use the following code:
>
> | client resp file |
> client := WebClient new.
> [resp := client httpGet: 'http://dl.dropbox.com/u/6204244/mcs-ftl.pdf'.
> resp isSuccess ifFalse:[^self error: resp status].
> "Stream the content from the response"

Add this line here: resp contentStream binary.


Levente

> file := FileStream newFileNamed: 'mcs-ftl.pdf'.
> resp streamTo: file size: resp contentLength progress: nil.
> file close] ensure: [client close]
>
> I took the code from the WebClient Responses help page and only slightly modified it.
>
> The problem is that I can open the file with Preview but all the pages are blank. The downloaded file has 9.9 MB while the original has 7.2 MB. So clearly something went wrong. Maybe some problem with the encoding?
>
> Any idea how to fix that? I'd be most thankful.
>
> Cheers,
> Bernhard
>

Reply | Threaded
Open this post in threaded view
|

Re: Download PDF using WebClient

Andreas.Raab
In reply to this post by bpi
On 10/25/2010 2:20 PM, Bernhard Pieber wrote:

> Dear Squeakers,
>
> I have a problem downloading a PDF file using WebClient. I use the following code:
>
> | client resp file |
> client := WebClient new.
> [resp := client httpGet: 'http://dl.dropbox.com/u/6204244/mcs-ftl.pdf'.
> resp isSuccess ifFalse:[^self error: resp status].
> "Stream the content from the response"
> file := FileStream newFileNamed: 'mcs-ftl.pdf'.
> resp streamTo: file size: resp contentLength progress: nil.
> file close] ensure: [client close]
>
> I took the code from the WebClient Responses help page and only slightly modified it.
>
> The problem is that I can open the file with Preview but all the pages are blank. The downloaded file has 9.9 MB while the original has 7.2 MB. So clearly something went wrong. Maybe some problem with the encoding?
>
> Any idea how to fix that? I'd be most thankful.

Make the file #binary before downloading. Otherwise you'll end up with
crlf, utf8 and whatnot conversions.

Cheers,
   - Andreas