用rails上传文件时会覆盖吗?
我使用以下代码在rails中上传文件:
name = upload['datafile'].original_filename
directory = "public/data/#{id}/#{app_id}"
Dir.mkdir("#{RAILS_ROOT}/public/data/#{id}/#{app_id}")
path = File.join(directory, name)
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
有人可以告诉我如何告诉rails,它应该在文件已经存在时覆盖该文件吗?
谢谢!
i upload files in rails with following code:
name = upload['datafile'].original_filename
directory = "public/data/#{id}/#{app_id}"
Dir.mkdir("#{RAILS_ROOT}/public/data/#{id}/#{app_id}")
path = File.join(directory, name)
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
can someone tell me how i can tell rails, that it should overwrite the file when it already exists?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该检查目录是否已经存在:
You should check if a dir already exist: