显示上传成功后上传的图像
Showing uploaded image after successful upload
我在Magnific Popup(灯箱)中使用Carrierwave宝石。我想做的是在上传图像后,它将显示新上传的图像。
目前,从灯箱上传图像后,它将继续显示您最初查看的图像,并显示"成功"消息,表示您的图像已成功上传。
当前的设置是用户点击一张照片,打开一个带有其图库照片的灯箱。在灯箱中,您可以删除当前照片,上传新照片,或将其作为您的头像。
photos.js.coffee:
jQuery ->
$('form#new_photo').fileupload
dataType: "script"
add: (e, data) ->
file = data.files[0]
data.context = $(tmpl("template-upload", file))
$('#new_photo').append(data.context)
data.submit()
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
stop: (e, data) ->
$('.upload').hide()
照片控制器:
def create
@photo = Photo.create(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to :back
else
render :action => 'new'
end
end
show.html.slim:
-if @user.photos.present?
.slider_container
h4 Photos
a.left_arrow href="#"
img alt="" src="/assets/left_arrow.png" /
ul.slider.parent-container
= hidden_field_tag 'current_index',0
-@user.photos.each_with_index do |photo, index|
li class="#{index > 2 ? 'hide' : ''}"
= link_to image_tag(photo.image_url(:thumb)), "#" + dom_id(photo)
div id="#{dom_id(photo)}" class="mfp-hide"
center
= image_tag(photo.image_url(:popup))
- if @user == current_user
= button_to('Set as Profile Image', [:avatar, photo], class: 'btn')
'
= button_to "remove", photo, :confirm => 'Are you sure?', :method => :delete, class: 'btn'
= form_for Photo.new do |f|
= f.label :image, "Upload photos:"
= f.file_field :image, multiple: true, name: "photo[image]"
script#template-upload type="text/x-tmpl"
.upload
| {%=o.name%}
.progress
.bar style=("width: 0%")
就我个人而言,我更喜欢使用http://blueimp.github.io/jQuery-File-Upload/
- 复杂的任务,如停止上传请求
- 使用浏览器上载而不上载XHR文件
- 异步上载
它还有文档https://github.com/blueimp/jQuery-File-Upload/wiki/API
如果您想自己完成,请将json与图像url一起发送回。
var imageurl = "";
$('#inputValue').attr("src", imageurl);
例如,我将输入值绑定到url
$('#inputName').bind('input',function(){
$('#inputValue').attr("src", $(this).val());
});
Demohttp://jsfiddle.net/margusmartsepp/83w51u85/
您的操作非常复杂。。
这是一个简单的解决方案。编写javascript函数,使用ajax发送选定的图像。
<script>
var client = new XMLHttpRequest();
function upload_banner()
{
var file = document.getElementById("file");
var formdata = new FormData();
formdata.append("file", file.files[0]);
var files=document.getElementById('file').files;
var file_name=files[0].name;
//alert(file_name);
var file_extension=file_extension_get(file_name);
if( file_extension == "jpg" || file_extension == "JPG" || file_extension == "jpeg" || file_extension == "JPEG" || file_extension == "gif" || file_extension == "GIF" || file_extension == "png" || file_extension == "PNG" )
{
client.open("post", "small_img.php", true);
client.send(formdata); /* Send to server */
}
else
{
alert("Wrong file format");
}
}
/* Check the response status */
client.onreadystatechange = function()
{
alert(client.readyState+"sssss"+client.status);
if (client.readyState == 4 && client.status == 200)
{
var m=client.responseText.toString();
$("#upload_img_view").html(m);
upload_id();
}
}
function file_extension_get(file)
{
return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
}
function upload_id()
{
var uploadid=document.getElementById('upload_id').value;
$("#banner_id").val(uploadid);
return true;
}
</script>
++++++++++++++++++++++++++++++++++++++++++++++++++++
在Html文件中编写以下代码以上传文件。。
<input type="file" id="file" name="file" onchange="upload_banner()" required name="file_create_video"> <br>
<div class="uplod-img" id="upload_img_view">
++++++++++++++++++++++++++++++++++++++++++++++++++
在php文件中使用以下代码small_img.php
$path = "/var/www/upload/profile_pic/";
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
echo "<img src='$u' height='100%' width='100%'> <input type='hidden' id='upload_id' name='upload_id' value='$k' >";
}
这对我有用…试试看…根据你的代码
相关文章:
- 可以'尽管上传成功,但在aws3中找不到图像
- 从AJAX成功函数更新图像源
- 在成功jqPuzzle上更改图像
- 成功后如何使用AJAX在TD中显示值和图像
- Ajax 完整函数即使 json 响应成功也不会设置背景图像
- 使用 JQuery 更改背景图像是成功的,但如何使一个图像默认
- 请上传 显示成功,但图像不在上传目录中
- 使用AJAX传递图像文件不成功
- 表单验证成功后加载图像
- Jquery在ajax上设置onclick null并更改按钮图像成功
- 显示上传成功后上传的图像
- 如何检测图像加载失败,如果失败,尝试重新加载直到成功
- 显示加载gif图像,直到函数成功
- Javascript Ajax成功图像替换
- Javascript跨域图像加载检查加载图像是否加载成功
- 使用Node.js在http请求成功后记录图像
- 如何防止ajax成功显示图像破碎前的图标
- ContentFlow Javascript-成功使用图像以外的内容
- 如何将按钮替换为加载gif图像当单击提交按钮,然后在成功删除表单上,并替换为成功消息时
- JQuery:在成功加载时显示图像,并与加载冲突