【问题标题】:Why does this file upload code corrupt my MP3 files?为什么这个文件上传代码会损坏我的 MP3 文件?
【发布时间】:2016-02-24 06:30:25
【问题描述】:

我有一个基于 Sinatra 的项目,带有 page,用户可以在其中上传 MP3 文件。

<h2><%= I18n.t(:home_title) %></h2>
<%= I18n.t(:upload_body_text) %>
<form action="/<%= I18n.locale %>/upload" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="song" size="40">
</p>
<div>
<input type="submit" value="<%= I18n.t(:home_submit) %>">
</div>
</form>

上传由这个route处理:

post "/upload" do 
  File.open('uploads/' + params['song'][:filename], "w") do |f|
    f.write(params['song'][:tempfile].read)
  end
  erb :main
end

文件上传后已损坏:

  1. Windows Media Player 中 MP3 文件的图像失真。
  2. 声音已损坏(听起来不对)。

我该如何解决?

【问题讨论】:

  • 你是在 Windows 机器上做 File.open 吗?
  • @muistooshort 是的,Windows 7。

标签: ruby file-upload upload sinatra


【解决方案1】:

您正在以文本模式(默认)打开文件:

File.open('uploads/' + params['song'][:filename], "w")

但是您正在编写二进制数据(MP3)。需要在binary mode中打开目标文件:

"b"  Binary file mode
     Suppresses EOL <-> CRLF conversion on Windows. And
     sets external encoding to ASCII-8BIT unless explicitly
     specified.

否则 IO 库将尝试将 EOL 转换为 Windows 样式的 CR-LF 对:

File.open('uploads/' + params['song'][:filename], "wb")
# --------------------------------------------------^

此外,您不应该使用用户提供的名称 (params['song'][:filename]) 作为文件名而不彻底清理它;或者更好的是,根本不使用他们的名字,将他们的名字存储在某个数据库中,并使用表的id 作为文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 2023-04-10
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2010-10-31
    • 1970-01-01
    相关资源
    最近更新 更多