Can't read a file from an input type='file' tag in chunks. #94

Closed
kektre opened this Issue Oct 17, 2010 · 7 comments

Projects

None yet

3 participants

@kektre
Contributor
kektre commented Oct 17, 2010

In sinatra 1.0 a file read (from an input type='file' name='f'' tag) and saved this way works fine.

 File.open(path, "w") do |buff|
   while blk = params[:f][:tempfile].read(65536)
     buff.write(blk)
   end
end

In sinatra 1.1 master this doesn't work (the created file is empty). Only possible way is to use:
File.read(params[:f][:tempfile])

@rkh
Member
rkh commented Oct 17, 2010

Are you running both on the same rack version?

@kektre
Contributor
kektre commented Oct 17, 2010

Rack version 1.2.1, Ruby 1.9.2

@rkh
Member
rkh commented Oct 17, 2010

Thanks, will look into this.

@gnandretta
Member

Hi, I think I've found where the problem is gnandretta@509599b

@kektre
Contributor
kektre commented Oct 18, 2010

with the patch above I am getting: Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8 exception when writing in chunks

edit: when using the "wb" flag for File.open, with the patch applied it saves files correctly. Although this is kind of weird, because I am on linux and the "b" flag is windows specific only afaik(?).

@rkh
Member
rkh commented Oct 18, 2010

Yeah, the input stream is ASCII-8BIT but you're trying to write to a file in unicode, "wb" sets encoding to binary in 1.9. This is not related to your OS. Even though your OS/file system might not care, 1.9 is encoding aware.

@rkh
Member
rkh commented Oct 18, 2010

Merged ohhgabriel's fix (thanks). Dealing with the encoding in this case has either to be done on application layer, or if it can be detected from the incoming request, at rack layer. Depending on the type of upload, you should just stick with "wb" and eventually handle encoding later.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment