I am developing an online calendar service. Because of some reasons, it needs about 15s to create the ics file. So I wanna using Ajax loading in the web-front and start downloading when the file is ready. I really not sure how to connect these two. Can any one give me an example code?
1 Answer
var f = function() { jQuery.ajax('/path/to/your/file.ics', { type : 'HEAD', async : true, success : function(data, status, xhr) { window.location.href = '/path/to/your/file.ics'; window.clearInterval(f); } }); }; window.setInterval(f, 1000); Just set 1000 to the amount of milliseconds to refresh, whether the file exists and /path/to/your/file.ics to the path of your ics file.
3 Comments
Ciel
Hi, thank you! But there is a problem. If ics file doesn't exits in the disk(only exit in the memory), how can I do? Should I write it in to a temp disk first?
Matteo B.
@Ciel That totally depends on your means. I once made a download page, that was completely "emulated" in PHP (the server sent the correct responses but never had a file on the disk). The problem here generally is: JavaScript has almost no way to recieve notifications from a server. Assuming it isn't local, the only two ways I know are: 1) Frequently checking for updates and 2) setting a high timeout for the request and waiting for a response. Only the first requires no server side changes in this case. But since i don't know your server details, I can't tell you how to do it the second way.
Matteo B.
@Ciel So if you like it easy: Yep, writing it to the disk would be a good idea :)