title: Download file by url in lua
body: <p>Lua beginner here. :)</p>
  
  <p>I am trying to load a file by url and somehow I am just too stupid to get all the code samples here on SO to work for me.</p>
  
  <p><a href="https://stackoverflow.com/questions/11479783/how-to-download-a-file-in-lua-but-write-to-a-local-file-as-it-works">How to download a file in Lua, but write to a local file as it works</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11413127/downloading-and-storing-files-from-given-url-to-given-path-in-lua">downloading and storing files from given url to given path in lua</a></p>
  
  <pre><code>socket = require("socket")
  http = require("socket.http")
  ltn12 = require("ltn12")
  
  local file = ltn12.sink.file(io.open('test.jpg', 'w'))
  http.request {
      url = 'http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg',
      sink = file,
  }
  </code></pre>
  
  <p>my program runs for 20 - 30s and afterwards nothing is saved. There is a created test.jpg but it is empty.
  I also tried to add w+b to the <code>io.open()</code> second parameter but did not work.</p>