开发者

FileUtils.mv adding linebreaks in Windows

I am streaming wav data from a flash application. If I get the data and do the following:

f = File.open('c:/test.wav', 'wb')
f << w开发者_运维知识库av_data.pack('c'*wav_data.length)
f.close

The wav file works perfectly. If I do this:

f = Tempfile.new('test.wav')
f << wav_data.pack('c'*wav_data.length)
f.close
FileUtils.mv(f.path, 'c:/')

The file is there, but sounds all garbled. Checking in a hex editor shows that everywhere the working file had an 0A (or \n), the garbled version had 0D0A (or \r\n)

I am using this in conjuction with rails+paperclip, and am going to be using a combination of Heroku and S3 for the live app, so I am hoping this problem will solve itself, but I'd like to get this working on my local machine for the time being.

Does anybody know of any reason FileUtils.mv would be doing this, and if there is a way to change its behaviour?


It looks like a binary file vs text file problem. You should set your file to binary with File#binmode as in:

f = Tempfile.new('test.wav')
f.binmode
f << data

You must have had a second argument to open in your first example, no? Something like "w+b", probably... The "b" stands for binary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜