---
title: "不透過 HTTP 將檔案附加到 ActiveStorage"
description: "如果你想附加一個在磁碟上產生的檔案，或是從使用者提交的 URL 下載下來的檔案，可以這樣做。"
canonical: https://oozou.com/til/attaching-file-to-activestorage-without-http-19
locale: zh-Hant
date: 2020-10-13
topic: ruby-on-rails
---

# 不透過 HTTP 將檔案附加到 ActiveStorage

> 如果你想附加一個在磁碟上產生的檔案，或是從使用者提交的 URL 下載下來的檔案，可以這樣做。

Published: 2020-10-13 · Topic: [ruby on rails](https://oozou.com/zh-Hant/til-topics/ruby-on-rails) ([markdown](https://oozou.com/zh-Hant/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-Hant/contact

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