简体   繁体   English

ruby on rails:关于文件上传和显示文件

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

Currently, we can save various files using database and S3. 当前,我们可以使用数据库和S3保存各种文件。 However, it is not possible to display those files well. 但是,不可能很好地显示这些文件。

The code below is the showviewfile code. 下面的代码是showviewfile代码。 And there are two things I want to do here. 我想在这里做两件事。

     <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>

The first is to display the saved file for each format. 第一种是显示每种格式的保存文件。 Second, see the image below. 其次,请参见下图。 It means that audio files are not displayed properly. 这意味着音频文件无法正确显示。

在此处输入图片说明

So, I'd like to solve these problems, but I do not know how to solve it. 因此,我想解决这些问题,但是我不知道如何解决。 How can we solve it? 我们该如何解决呢?

model file 模型文件

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"] }

please tell me!! 请告诉我!!

I see at least a problem in your conditions: alpha || 在您的情况下,我至少看到了一个问题:alpha || beta will just evaluate as beta if alpha is "true-ish". 如果alpha为“真假”,则beta只会评估为beta。

Try changing your conditionals to something like 尝试将条件更改为类似

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

This is probably why it's giving you an image instead of playing an audio file appropriately. 这可能就是为什么它为您提供图像而不是正确播放音频文件的原因。 The way you have it, unless your file is mp4, gif, or mpegaudio, you will get an image. 除非您的文件是mp4,gif或mpegaudio,否则您将得到图像。

What library do you use for uploading ? 您使用哪个库上载?

If it's carrierwave / paperclip maybe try to give @listing.file.url as a parameter instead ? 如果是载波/回形针,则可以尝试使用@ listing.file.url作为参数来代替?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM