---
title: "不经过 HTTP 把文件附加到 ActiveStorage"
description: "如果你想附加一个在磁盘上生成的文件，或者从用户提交的 URL 下载下来的文件，可以这样做。"
canonical: https://oozou.com/til/attaching-file-to-activestorage-without-http-19
locale: zh-Hans
date: 2020-10-13
topic: ruby-on-rails
---

# 不经过 HTTP 把文件附加到 ActiveStorage

> 如果你想附加一个在磁盘上生成的文件，或者从用户提交的 URL 下载下来的文件，可以这样做。

Published: 2020-10-13 · Topic: [ruby on rails](https://oozou.com/zh-Hans/til-topics/ruby-on-rails) ([markdown](https://oozou.com/zh-Hans/til-topics/ruby-on-rails.md))

如果你想附加一个在磁盘上生成的文件，或者从用户提交的 URL 下载下来的文件，可以这样做。

```ruby
@model.image.attach(
    io: File.open('/path/to/file'),
  filename: 'file.pdf',
  # content type is optional
  content_type: 'application/pdf'
)
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/zh-Hans/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
