Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Short Answer</h1> <p>There is no way to specify a <code>Content-Type</code> value in the filesystem when you create your file. In fact, this is probably not the best way to achieve your goal. </p> <p>In order to suggest that a browser download a file rather than displaying it, you can leave <code>Content-Type: text/csv</code> and add the header <code>Content-Disposition: attachment</code> or <code>Content-Disposition: attachment; filename=&lt;your custom filename&gt;.csv</code> to change the filename in the "Save As..." dialog.</p> <h1>Setting <code>Content-Disposition</code> using Paperclip and AWS::S3</h1> <p>To set the <code>Content-Disposition</code> header using Paperclip, you can add a key to your <code>has_attached_file</code> definition: <a href="http://rubydoc.info/gems/paperclip/2.4.5/Paperclip/Storage/S3"><code>s3_headers</code></a>.</p> <pre><code>has_attached_file :spreadsheet, :path =&gt; 'perhaps/a/custom/path/:class/:id/:filename', :or_maybe =&gt; 'other parameters', :s3_headers =&gt; { 'Content-Disposition' =&gt; 'attachment' } </code></pre> <h2>Content-Type issues</h2> <p>By default, a file with the extension <code>.csv</code> should be classified as a <code>text/csv</code> file. You can check this with <code>Mime::Type.lookup_by_extension('csv').to_s # =&gt; "text/csv"</code>. If this is not the case, you can add text/csv as a custom mime-type by creating a <code>config/initializers/mime_types.rb</code> file and adding:</p> <pre><code>Mime::Type.register 'text/csv', :csv </code></pre> <p>However, this should almost always not be the case (unless Windows does something funky with content types; I've only tested in Linux).</p> <h1>Examples</h1> <p>I've put up two examples that you can check. The first is a CSV file uploaded with a <code>text/plain</code> mime-type which forces the browser to show it in-browser without downloading (my browser downloaded <code>text/csv</code> files).</p> <p><a href="https://s3.amazonaws.com/stackoverflow-demo/demo.csv">https://s3.amazonaws.com/stackoverflow-demo/demo.csv</a></p> <p>The second also has a mime-type of <code>text/plain</code>, but I added a header <code>Content-Disposition: attachment; filename="mycustomname.csv"</code></p> <p><a href="https://s3.amazonaws.com/stackoverflow-demo/demo-download.csv">https://s3.amazonaws.com/stackoverflow-demo/demo-download.csv</a></p> <p>You'll notice that the first link is displayed in browser, while the second link is downloaded with the custom name <code>mycustomname.csv</code>.</p> <p>To learn why, look at the headers using <code>curl -I</code>.</p> <pre><code>$ curl -I https://s3.amazonaws.com/stackoverflow-demo/demo-download.csv HTTP/1.1 200 OK Content-Disposition: attachment; filename="mycustomname.csv" Content-Type: text/plain </code></pre> <p>versus</p> <pre><code>$ curl -I https://s3.amazonaws.com/stackoverflow-demo/demo.csv HTTP/1.1 200 OK Content-Type: text/plain </code></pre> <p>Note: unrelated headers were removed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload