NodeMCU-HTTP-Server icon indicating copy to clipboard operation
NodeMCU-HTTP-Server copied to clipboard

Issue in `Res:sendFile()`

Open DTJF opened this issue 4 years ago • 0 comments

I've an issue in function Res:sendFile() in the local function doSend(): When the file length is a multiple of 512 (1024, 1536, 2048, ...) the function fails at the end of the file, since buf is nil in self._skt:send(buf). Here's how I fixed it:

  print('* Sending ', filename)
  local pos, fin = 0, nil
  local function doSend()
    file.open(filename, 'r')
    if file.seek('set', pos) == nil then
      fin = 1
    else
      local buf = file.read(512)
      pos = pos + 512
      if buf == nil then
        fin = 1
      else
        self._skt:send(buf)
      end
    end
    file.close()
    if fin then
      self:close()
      print('* Finished ', filename)
    end
  end

Great project, thanks for sharing!

DTJF avatar Aug 05 '20 13:08 DTJF