【问题标题】:jQuery file upload with paperclip, showing preview image before upload使用回形针上传 jQuery 文件,在上传前显示预览图像
【发布时间】:2015-08-01 03:13:18
【问题描述】:

我正在努力完成this

尝试显示我要上传的图像的预览,但没有显示任何内容。我正在查看我的日志,当我尝试上传时,日志会吐出一些内容,主要是 Image Exists (0.2ms)Image Load (0.3ms),并显示 SELECT 1 AS.... SQL 语法

这是我的 form.html.erb

<%= simple_form_for @photo, html: { multipart: true, id: 'bePhoto' } do |f| %>    
  <div class="row fileupload-buttonbar">
    <div class="col-lg-7">
        <!-- The fileinput-button span is used to style the file input field as button -->
        <span class="btn btn-success fileinput-button">
            <i class="glyphicon glyphicon-plus"></i>
            <span>Add files...</span>
            <input type="file" name="photos[]" multiple>
        </span>
        <button type="submit" class="btn btn-primary start">
            <i class="glyphicon glyphicon-upload"></i>
            <span>Start upload</span>
        </button>
        <button type="reset" class="btn btn-warning cancel">
            <i class="glyphicon glyphicon-ban-circle"></i>
            <span>Cancel upload</span>
        </button>
        <button type="button" class="btn btn-danger delete">
            <i class="glyphicon glyphicon-trash"></i>
            <span>Delete</span>
        </button>
        <input type="checkbox" class="toggle">
        <!-- The global file processing state -->
        <span class="fileupload-process"></span>
    </div>

    <!-- The global progress state -->
    <div class="col-lg-5 fileupload-progress fade">
        <!-- The global progress bar -->
        <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
            <div class="progress-bar progress-bar-success" style="width:0%;"></div>
        </div>
        <!-- The extended global progress state -->
        <div class="progress-extended">&nbsp;</div>
    </div>
</div>
<!-- The table listing the files available for upload/download -->

<table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>

<% end %>

然后我有这个 javascript

$(function () {
    $('#bePhoto').fileupload();

    $('#bePhoto').addClass('fileupload-processing');
      $.ajax({
          // Uncomment the following to send cross-domain cookies:
          //xhrFields: {withCredentials: true},
          url: $('#bePhoto').fileupload('option', 'url'),
          dataType: 'json',
          context: $('#beTripForm')[0]
      }).always(function () {
          $(this).removeClass('fileupload-processing');
      }).done(function (result) {
          $(this).fileupload('option', 'done')
              .call(this, $.Event('done'), {result: result});
    });    
});

我也有这个

<script src="http://blueimp.github.io/JavaScript-Templates/js/tmpl.min.js"></script>
<script src="http://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<script src="http://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js"></script>
<script src="http://blueimp.github.io/Gallery/js/jquery.blueimp-gallery.min.js"></script>

还有这个:

//= require jquery-fileupload/basic
//= require jquery-fileupload/basic-plus

我几乎试图模仿示例演示,但不确定我是否正确地这样做。

【问题讨论】:

  • 你的 Probloem @hellomello 是什么?服务器端或只是在上传前显示预览图像。
  • 5minutenpause.com/blog/2013/09/04/…这可能对你有帮助
  • @bipashant 服务器端,我看不到预览
  • 它是自动上传的,我认为它不应该上传,除非我也按下上传按钮
  • 谈你的需求你只需要预览选择的文件就可以了?

标签: javascript jquery ruby-on-rails ruby-on-rails-4 jquery-file-upload


【解决方案1】:

如果您关心的是在上传之前显示所选图像并在表单提交期间上传它们,我尝试为此编写自定义脚本。请查看并告诉我。

这里我读取了输入文件的内容,并以缩略图的形式显示在图像标签中。它不显示文件上传的进度,但所有文件都是在表单提交事件期间提交的。

请添加一个 div 以在文件输入字段上方显示缩略图

    <div class="row" id="uploader-wrapper"></div>

    <%= f.file_field(:photo, id: 'photo_upload_btn', multiple: true) %>

并为您的文件输入字段添加事件监听器。

$("#photo_upload_btn").change(function () {
        displayThumbnail(this);
});

添加以下 Javasript 代码以显示缩略图

function displayThumbnail(input) {
        for( var i = 0;i<input.files.length;i++){
            if (input.files && input.files[i]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    if ($('#photo_upload_btn').valid()) {
                        var $newImageThumbnail = makeElement('img',{ class: "image-frame",src: e.target.result});
                        $('#uploader-wrapper').append($newImageThumbnail);
                    }
                };
                reader.readAsDataURL(input.files[i]);
            }
        }

    }

    function makeElement(element, options) {
        var $elem = document.createElement(element);
        $.each(options, function (key, value) {
            $elem.setAttribute(key, value);
        });
        return $elem;
    }

别忘了给缩略图设置样式

.image-frame {
  border: 1px dashed #333;
  width: 150px;
  height: 150px;
  margin: 0 0 10px;
}

【讨论】:

  • 抱歉,回复晚了...我收到一个错误Uncaught TypeError: $(...).valid is not a function... 然后它指向我:if ($('#photo_upload_btn').valid()) {
  • 哦,天哪,我必须在我的应用程序末尾添加一个验证 jquery 脚本才能正常工作!有用!非常感谢,我想我现在必须弄清楚如何添加进度条......嗯谢谢
  • 我不确定这是否应该发生,但是当我选择一个图像,然后单击文件字段以选择更多图像时,仅上传了最后选择的图像。 . (即,如果我选择了 1 张图片,然后再次单击文件字段,要再选择 2 张图片,则仅上传了最后 2 张图片。)
  • Yaa 我们无法使用这种方法维护以前的文件,请查看stackoverflow.com/questions/20537696/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 2015-07-02
  • 2012-06-13
  • 2012-10-05
  • 2020-12-03
  • 1970-01-01
  • 2012-09-21
相关资源
最近更新 更多