显示上传的图像没有页面刷新?

时间:2016-03-23 06:51:24

标签: image-uploading

我在没有页面加载的情况下将图像上传到服务器。我的问题是,在没有页面加载的浏览器中成功上传后,显示它的最佳方法是什么?

   html part---->
     <form method="post" data-remote="true" accept-charset="UTF-8" action="/update_profile_image" enctype="multipart/form-data" id="edit_user_72" class="edit_user">

      <div>
         <input type="file" id="user_avatar" name="user[avatar]" as="file">
       </div>
     <input type = "button" value ="Update" id= "update-img" 
      onclick ="AjaxUpdateImage($('form').attr('action'),$('form').attr('id'))" />
</form>


        ajax part--->

       function AjaxUpdateImage(url,id) {
       var formData = new FormData($('#'+id)[0]);
       $.ajax({
       type: "POST",
       url: url,
       processData: false, // Don't process the files
       contentType: false, // Set content type to false as jQuery will tell the server its a query string request
       data: formData, // serializes the form's elements.
       success: function(data)
       {
           console.log(data);
       },
       error: function (data)
       {
            console.log(data)
       }
  })



   server part (ruby on rails)---->

       def update_profile_image
        id = params[:user][:attr].to_i
        @user = User.find(id)
        @response = @user.update_attribute(:avatar, params[:user][:avatar])
        respond_to do |format|
        format.json { render json: @response }
       end
      end

0 个答案:

没有答案
相关问题