简体   繁体   English

如何使用jQuery File Upload更新上传的图像?

[英]How to update uploaded image with jQuery File Upload?

I am using jQuery File Upload for PHP and it works well. 我正在使用PHP的 jQuery文件上传 ,效果很好。 The only problem is that when I open two tabs in the browser of the same page (index.html) and I upload a photo in tab 1 and I see the uploaded photo but when I go to tab 2 there is no the uploaded photo. 唯一的问题是,当我在同一页面的浏览器中打开两个选项卡(index.html)时,我在选项卡1中上传了一张照片,并且看到了上传的照片,但是当我转到选项卡2时 ,则没有上传的照片。 I have to refresh the page to see it. 我必须刷新页面才能看到它。

So I tried to solve the problem in this way: 因此,我尝试通过以下方式解决问题:

setInterval(function() {
    $.ajax({
    url: $('#fileupload').fileupload('option', 'url'),
        dataType: 'json',
        context: $('#fileupload')[0]
    }).always(function () {
        $(this).removeClass('fileupload-processing');
    }).done(function (result) {
        $(this).fileupload('option', 'done').call(this, $.Event('done'), {
            result: result
        });
    });
}, 3000);

Works but not very well. 可行,但不是很好。 Every three seconds I get all uploaded images repeated (not the one I just uploaded). 每三秒钟重复一次所有上传的图像(不是我刚刚上传的图像)。 I also tried to remove all list of images $("ul.files").find('.template-download').remove(); 我还尝试删除所有图像列表$("ul.files").find('.template-download').remove(); and then add them so in this way the images are not repeated. 然后添加它们,这样就不会重复图像。 But as I said doesn't work very well. 但是正如我所说,效果不是很好。

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

You are uploading photos in every tab every 3 seconds?? 您每3秒在每个标签中上传一次照片吗?? You are aware that that's just a waste of bandwidth and server processing, right? 您知道那只是浪费带宽和服务器处理,对吗?

Of course your images get repeated, because to the server, every time this code executes is a new image. 当然,您的图像会重复出现,因为对于服务器而言,此代码每次执行都是一个新图像。 Wouldn't it be better if you created a method that just refreshes de page? 如果您创建了一种刷新页面的方法,会更好吗? I mean, I suppose you store your uploaded photos in the server (otherwise you wouldn't be able to see them in the other tabs). 我的意思是,我想您将上传的照片存储在服务器中(否则您将无法在其他选项卡中看到它们)。 Just poll the server to know if there are new photos uploaded every three seconds. 只需轮询服务器以了解是否每三秒钟上传一次新照片。

Or even better, if you're targeting html5-compliant browswers that support the sessionStorage object, you could add (or update) a property there. 甚至更好的是,如果您要针对支持sessionStorage对象的html5兼容浏览器,则可以在那里添加(或更新)属性。 Any other tab in the same domain will immediately receive an event that such property was added / modified. 同一域中的任何其他选项卡将立即收到添加/修改此类属性的事件。 You can use this to know when a new image has been uploaded. 您可以使用它来知道何时上传新图像。

//when the image has uploaded...
sessionStorage.setItem('images', 'number-of-images');

And if you have an event handler like this in your page, you'll get notified when this property changes, indicating you can refresh your page and get the new images: 而且,如果页面中有这样的事件处理程序,则此属性更改时会通知您,表明您可以刷新页面并获取新图像:

window.addEventListener('storage', function(e) {
    if (e.key == 'images') { //key 'images' updated.
         //refresh page via AJAX.
    }
}, false);

Ideally, you would use this method if sessionStorage is available, and default to polling the server if it is not: 理想情况下,如果sessionStorage可用,则应使用此方法;如果不可用,则默认轮询该服务器:

if (window.sessionStorage) { 
     //sessionStorage support, use the event.
} else {
    //default to polling the web server for updates
}

Hope this helps you. 希望这对您有所帮助。

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

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