簡體   English   中英

ruby on rails:關於文件上傳和顯示文件

[英]ruby on rails : about file upload and show file

當前,我們可以使用數據庫和S3保存各種文件。 但是,不可能很好地顯示這些文件。

下面的代碼是showviewfile代碼。 我想在這里做兩件事。

     <div class="listing_content">
            <% if @listing.file_content_type == 'video/mp4' then %>
              <%= video_tag @listing.file, :controls => true, width: "640", height: "360" %>
            <% elsif @listing.file_content_type == "image/jpg" || "image/jpeg" || "image/png" || "image/gif" then %>
              <%= image_tag @listing.file, :width => 640, :height => 340 %>
            <% elsif @listing.file_content_type == 'audio/mpeg' || 'audio/x-mpeg' || 'audio/mp3' || 'audio/x-mp3' || 'audio/mpeg3' || 'audio/x-mpeg3' || 'audio/mpg' || 'audio/x-mpg' || 'audio/x-mpegaudio' then %>
              <%= audio_tag @listing.file, :controls => true %>
            <% else %>
              <%= image_tag @listing.file %>
            <% end %>
          </div>

第一種是顯示每種格式的保存文件。 其次,請參見下圖。 這意味着音頻文件無法正確顯示。

在此處輸入圖片說明

因此,我想解決這些問題,但是我不知道如何解決。 我們該如何解決呢?

模型文件

validates_attachment :file, content_type: { content_type: ['audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio', 'video/mp4', 'text/plain', 'application/msword', 'application/vnd.ms-powerpoint', 'application/pdf', "image/jpg", "image/jpeg", "image/png", "image/gif"] }

請告訴我!!

在您的情況下,我至少看到了一個問題:alpha || 如果alpha為“真假”,則beta只會評估為beta。

嘗試將條件更改為類似

['audio/mpeg', 'audio/x-mpeg', 
 'audio/mp3', 'audio/x-mp3'].include?(@listing.file_content_type)

這可能就是為什么它為您提供圖像而不是正確播放音頻文件的原因。 除非您的文件是mp4,gif或mpegaudio,否則您將得到圖像。

您使用哪個庫上載?

如果是載波/回形針,則可以嘗試使用@ listing.file.url作為參數來代替?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM