JS detect upload from a new window - javascript

I have a windows, which in there is a upload button. When user click this button, it will popup a page for user to upload the desired file. But i dont know how can the main page detect if the user uploaded and add a field to the main form?
I've been seeing this somewhere but i dont remember so i can't go back to check out the source JS..
If anyone know, please give me an advice.
Thanks in advance :)
PS: im working on Jquery

A variation of what moxn said,
The popped up window accepts the file upload, it sends the file to the server and then returns another page indicating whether the file was uploaded successfully.
I don't know what server side language you are using but, on that return page you should have something like:
if (file was uploaded successfully){
print "<html>
<body onload='window.opener.trigger()'>
file uploaded successfully, please close this window
</body>
</html>";
}else{
print "<html>
<body>
could not upload file due to [reason], please re-upload the file
</body>
</html>";
}
Notice the body onload='' on the first part of the conditional...
Hope that helps

From your popup window you can access the parent window through window.opener.
$("#upload-button").click(function(evt){
window.opener.trigger();
});
would call a function trigger() in the parent window each time the upload button has been clicked.

Related

Is it possible to force download a file (like pdf) as soon as a page loads

Actually this is what I'm trying to do. I have a form and when a user fills that form it goes to a thank you page where thank you message is displayed. Now what I'm trying to do is as soon as the thank you page opens a pdf file should start downloading without the user clicking anywhere on the page. I tried to find a solution by searching but almost all of them have given a solution where one link is provided and user have to click that link to download her pdf. But that is not what I want. And I want to achieve that using Javascript or jQuery only, no server side language.
A clarification:- PDF download on clicking a link is already working. What I want is PDF to download as soon as thank you page opens. I know logically this should not happen because by doing this anyone can set any number of files to be downloaded as soon as their page opens and fill your local drive. But my client wants only this thing to happen.
Calling click() for the user on an other-wise working link should do the trick:
<html>
download pdf
<script>
var link = document.getElementById("link");
link.click()
</script>
</html>
You can do it, in the HEAD tag:
<meta http-equiv="refresh" content="0; url=yourfile.pdf">
This will work also with browsers without JavaScript, and as soon as the the head tags loaded.
a working example:
See an example on github
You can use ajax to hit the url when the page loads.
EDIT: You might want to check this out https://stackoverflow.com/a/29266135/4549494

Prevent PrettyPhoto from closing

the script i created uses PrettyPhoto to load an ifram on which there is an upload page going on, the upload works great the image is uploaded and the url is returned back to the page for preview, but not saved into the database just yet...
so far so good, i just want to add a function that if a picture is not saved and the user hits close button it will promt to either save or dismiss which it will make an ajax call to either remove the uploaded picture or save its url into the database
does anyone have an idea how to do that? not the ajax part i have everything under control, just need to know how to prevent the prettyphoto close button from closing directly
ps: i tried the callback function but what it does it that it closes then executes, but i need the execution to be done before closing
any help would be much appreciated
thank you
you can tweak the prettyPhoto js code but that will effect all your popups, so as you found the workaround of the close function inside the iframe then that's it, you didn't have to duplicate themes and hide the close button on the its stylesheet, you could have simply let jquery do so from withing the iframe, just include this code into your iframe
<script type="text/javascript">
$(document).ready(function() {
window.parent.$('.pp_close').css('display', 'none');
});
</script>

Is it possible to send an html file with a downloadable file in one response?

Basically, I am using an iframe to download a file. I set the source of the iframe to the file I want to download. That problem isn't that I can't get a download dialog. The problem is I can't close the window after the download is started.
What I was thinking is I could send back one file with this header.
"Content-Disposition","attachment;filename=test.txt"
Then I would like to send back another HTML for the browser to display. The whole point of the second file is to close the browse window.
BTW: It is far to late to go away from popups.
No, you can't send both a file and a page in the same response.
If you want two responses, you have to send two requests.
Alternatively, you can start the download, then use a timeout to close the window after the download has started:
window.location.href = 'test.txt';
window.setTimeout(function(){
window.close();
}, 100);
The whole point of the second file is to close the browse window.
You don't need this. Just setting that header on the response with the to-be-downloaded file is more than sufficient to get a Save As dialogue. You don't need to open the link in a new/blank window. Just let the link/form point to the desired URL and it'll open the Save As dialogue. The currently opened page will remain the same.

iFrame file upload with IE produces download dialogue

I'm developing a custom module for Drupal dealing with Document Management. At this point, the module loads, you can upload files (via a hidden iframe and some ajax requests), browse directories and set various levels of permissions. And everything works perfectly in Firefox.
Issue:
In IE however, I run into an issue. For some reason when I upload a file the save file dialogue pops up and asks me to download the file I just uploaded. It looks like it's asking me to download it from the web-servers tmp location however, as that seems to be the file-name and such. However, if I hit cancel and refresh the page I can see that the file I uploaded did actually get uploaded to the server.
Here is how the upload process works. Click the upload button. The upload button is the standard file input form element hidden and placed over a styled version of the button. Clicking on this causes the "Choose a file" dialogue to open.
Select a file and click "Open". A modal dialogue pops up asking you for some further information about the file. The modal is part of the same form element but remains invisible until you click Upload button
Clicking save in the modal causes the file to be uploaded. The "action" attribute on the form is pointed to a page and the target is the iframe.
The iFrame is polled a few times every second to see if it's contents have changed. When the file is uploaded a "success" message appears in the iframe. Since it belongs to the same domain, I scrape the content within the iframe.
Once the iFrame says that the file has been uploaded, I use some JS to update the application with the name of the file
Since I can't use Firebug in IE, I have to stick to utilizing Microsofts Web Developer Toolbar, which makes it very hard to figure out if the bolded step is actually occuring. It seems like it should, since the file IS being uploaded. It is just getting interrupted by that file download dialogue.
Since no one seems to have an answer, I'll post the fix I found. I doubt it's perfect, but it's the best solution I could come up with.
It turned out to be an issue with the polling as suspected. The fix ended up being instead of outputting json to the iFrame, just output some JavaScript that calls a function that updates the main window. That simple.

Close browser popup page after downloading file

When user clicks on a hyperlink, i am opening a popup window which downloads a file. Post download, it shows open/save dialog box. I want to close the popup window once the download is done (when user is prompted for saving the file).
I have tried window.close method, but it doesnt work as the context is not the popup window but the open/save dialog box.
I think you can not control it programatically. This is browser-specific thing where some browsers allow you to tick on a check box to close the window and so on.
you can do one thing:
after the "file writing code in your servlet or struts action use"
Step 1: call "response.flushBuffer();"
Step 2: call response.sendRedirect("close.htm")
where close.htm is :
<html>
<script>
window.close();
</script>
</html>
Only problem is identifying if the download is complete. I don't think there is a possible way of doing that. Anyway is there any use of keeping the pop-up open untill the download is complete. You can simply close the pop-up once the download is initiated. Using below JavaScript, can't you?
pop_up= window.open("", "PopUpName");
pop_up.document.write('POPUP TEXT');
pop_up.close();
Browsers have a habit of deciding for themselves whether to try to download a file or open it inside the browser window, depending on the browser used, plug-ins and server settings. It sounds like you might be opening the link in a new window, as if the browser was going to open the file rather than download it, and then the browser has opted for the download. This leaves the user with a downloaded file and a blank window that you have no control over.
To force it to download, you should be able to set the Content-Type header for the target of the link to application/force-download. How you do this will depend on your setup, and whether the file is downloaded directly (in which case it will be a server setting) or via PHP or .Net (in which case it's easy to programmatically set the header). Also make sure that the hyperlink doesn't have a target="_blank" attribute that opens the link in a new window.
It's Browser functionality, You can change browser setting for that. You shouldn't go for programming . By default If you will download file in firefox it ask for saving, but in crome It's doesn't ask.
You need to change setting of your browser.

Categories

Resources