I would like to download an image file from the server and save it on my PC by 
nim language.

At first, I wrote the following code:.
    
    
    import httpclient
    
    let url = "https://nnahito.com/images/nna_chara1.png";    # This is my server
    
    var client = newHttpClient()
    var response = client.get(url)
    
    writeFile("test.png", response.body)
    
    
    Run

However, the saved image file was not recognized as an image file.

\---

I looked it up and found the following article.

[https://forum.nim-lang.org/t/3673](https://forum.nim-lang.org/t/3673)

It says you can use "newFileStream".

So I rewrote the code as follows:.
    
    
    import httpclient
    import streams
    
    let url = "https://nnahito.com/images/nna_chara1.png";
    
    var client = newHttpClient()
    var response = client.get(url)
    
    var f = newFileStream("test.png", fmWrite)
    f.write(response.body)
    f.flush()
    
    
    Run

It worked fine.

\---

What is the difference between writeFile and newFileStream?

Also, is the Stream of newFileStream different from saving a file normally?

I would appreciate it if you could let me know if anyone knows.

Reply via email to