简体   繁体   English

如何在Rails中使用Ajax和Jquery将图像上传到Amazon AWS(S3)?

[英]How to upload an Image to Amazon AWS (S3) using Ajax and Jquery in Rails?

I want the user to upload their image from the site. 我希望用户从站点上载他们的图像。 I've done most of the things but now, I'm facing difficulties while uploading the image to the server. 我已经完成了大多数事情,但是现在,在将图像上传到服务器时遇到了困难。 How do I solve this issue? 我该如何解决这个问题?

HTML - HTML-

<div>
  <div style="height: 80px; position: relative;">
   <div id="target1">
    <% if !@aws_url3.nil? %>
     <img id="user_dl3" src="<%= @aws_url3 %>">
    <% end %>
  </div>
  <div id="close1" style="display: none;">
    <button id="closebtn1" class="closebtn">X</button>
  </div>
  <div id="closemsg1" style="display: none;  position: relative;">
    <button  id="deletebtn1" class="deletebtn">
        <p class="deletebtnstyle">Delete</p>
    </button>
    <button id="cancelbtn1" class="cancelbtn">
        <p class="cancelbtnstyle">Cancel</P>
    </button>
  </div>
  <div class="file_browser1" id="file_browser1">
    <label for="file-upload1" class="custom-file-upload z-depth-1">
        <i class="material-icons">&#xE147;</i>
        <p class="button-text"> Upload Here</p>
    </label>
  <form id="user_id1_form" action="">
    <input type="hidden" name="user_id1_key" value="<%= @aws_urls[0][:key] %>"/>
    <input id="file-upload1" type="file" accept="image/*" name="user_id1"/>
 </form>
 </div>
 </div>
 </div>
 <%= form_tag(user_url("#{@current_user.id}"), method: :put, id: "aws_key_form") do |f| %>
   <%= hidden_field_tag "aws_key1", @aws_urls[0][:key] %>
 <% end %>

Coffee Script - 咖啡脚本-

$ ->

    if $('#target1 img').length == 1
        $('#file_browser1').addClass 'none'
        $('#close1').addClass 'delete1'
        $('#closebtn1').on 'click', (e) ->
                $('#closemsg1').addClass 'msg1'     
        $('#cancelbtn1').click ->
                $('#closemsg1').removeClass 'msg1'
        $('#deletebtn1').click ->
                $('#close1').removeClass 'delete1'
                $('#closemsg1').removeClass 'msg1'
                $('#target1 > img').hide()
                $('#file_browser1').removeClass 'none'
                $('#file-upload1').val('')
                return

    $('#file-upload1').on 'change', (event) ->
        files = event.target.files
        image = files[0]
        console.log image.size
        reader = new FileReader
        $.ajax({
            type: "GET",
            url: "http://localhost:3000/users/get_url/13",
            success :(data) ->
                console.log data[0].url
                $.ajax({ 
                    url: data[0].url,
                    type: 'PUT',
                    xhr: -> 
                        myXhr = $.ajaxSettings.xhr();
                        if (myXhr.upload)
                            myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
                        return myXhr;
                    ,
                    success: (data) ->
                        alert "abcd",
                    error: (data) ->
                        alert data[0],
                    data: $('#file-upload1')[0].files[0],
                    cache: $.param(false),
                    contentType: "binary/octet-stream",
                    processData: $.param(false)
                }, 'json');
        })

        reader.onload = (file) ->

            img = new Image
            img.src = file.target.result
            $('#target1').html img
            $('#file_browser1').addClass 'none'
            $('#close1').addClass 'delete1'
            $('#closebtn1').on 'click', (e) ->
                    $('#closemsg1').addClass 'msg1'     
            $('#cancelbtn1').click ->
                    $('#closemsg1').removeClass 'msg1'
            $('#deletebtn1').click ->
                    $('#close1').removeClass 'delete1'
                    $('#closemsg1').removeClass 'msg1'
                    $('#target1 > img').hide()
                    $('#file_browser1').removeClass 'none'
                    $('#file-upload1').val('')
                    return
            return

        reader.readAsDataURL image
        console.log files
        return

PS - I've used Json to get the URL from the AWS-URL and the Image should be uploaded asynchronously as soon as the file is selected from the browser as there is no upload button available. PS-我已经使用Json从AWS-URL获取URL,并且一旦没有可用的上传按钮,就应该从浏览器中选择文件后立即异步上传图像。

Heroku have a detailed instruction how to do it. Heroku有详细的操作说明 I recommend using direct upload to S3. 我建议使用直接上传到S3。 In case you miss it there is autoUpload option. 万一您错过它,则有autoUpload选项。

暂无
暂无

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

相关问题 使用Rails 3.2和AJAX(非闪存上传解决方案)将多个文件直接上传到Amazon S3 - Uploading multiple files directly to Amazon S3 using Rails 3.2 and AJAX (non-flash upload solutions) 使用jQuery Ajax请求上传CORS Amazon S3文件 - CORS Amazon S3 file upload with jQuery Ajax request Rails使用aws-sdk gem和heroku上的jQuery-File-Upload直接进入S3上传 - Rails direct to S3 upload using aws-sdk gem and jQuery-File-Upload on heroku 使用上传到Amazon S3的JQuery文件在客户端调整图像大小 - Resizing image at client-side using JQuery file upload to amazon S3 如何使用jRecorder和Ruby on Rails将录制的音频文件上传到Amazon S3 - How to upload a recorded audio file to Amazon S3 using jRecorder and Ruby on Rails 将图像上传到Amazon S3(如何获取签名和策略文档) - Upload Image to Amazon S3 (How to get Signature and Policy Doc) 网络摄像头js将图像上传到Amazon S3 - Webcam js upload image to Amazon S3 使用 s3_direct_upload gem 的 Rails 6 Amazon S3 错误 POST 403(禁止) - Rails 6 Amazon S3 Error POST 403 (Forbidden) using s3_direct_upload gem 使用 AJAX 直接上传到亚马逊 S3 返回错误:Bucket POST 必须包含名为“key”的字段 - Upload Directly to amazon S3 using AJAX returning error: Bucket POST must contain a field named 'key' 使用 Ajax 请求(无 PHP)使用 HTML、javascript 和 jQuery 将图像上传到 Amazon s3 - Uploading Image to Amazon s3 with HTML, javascript & jQuery with Ajax Request (No PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM