Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Download file from browse form

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
Janusz
nuBuilder Team
Posts: 508
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 10 times
Been thanked: 18 times

Download file from browse form

Unread post by Janusz »

Hi.
When I want to download a file from the server to local computer - in case of EDIT form I am placing the following code inside the Button "onclick" and works fine :-):

Code: Select all

var f=$('#rap_location').val(); //get filename from the form
var link=sd_path+f; //sd_path defined as global variable

function download(dataurl, filename) {  
var a = document.createElement("a"); a.href = dataurl;  
a.setAttribute("download", filename);  
a.click();}

download(link, f);
I wanted to use similar code on the BROWSE form - to be executed when I click the specific row - but then my application wants to log out :-(
The following code is placed in the JS custome code:

Code: Select all

function download(dataurl, filename) {  
var a = document.createElement("a"); a.href = dataurl;  
a.setAttribute("download", filename);  
a.click();}

function nuSelectBrowse(e){
.... collecting link and file name 'f' is ok
download(link, f);
}
Probably I have some issue with the a.click() part of the code - but was trying some other possibilities but still unsolved.
"a" is generated properly when checking with console.log - but I do not know how to force "a" to be executed when I call the "download" function.
Do you have some suggestions how to modify the code to make it run properly from the Browse form?

When I use window.open (link,"myWindow"); it works fine in every place - but generally it opens files in the browser but I want to force them to be always downloaded.
If you like nuBuilder, please leave a review on SourceForge
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Download file from browse form

Unread post by kev1n »

Hi Janusz,

Something like this works perfectly for me.
When a download link is clicked, the file is downloaded. Otherwise, the record is opened.

Code: Select all

if(nuFormType() == 'browse'){
$("#nucell_0_0").html('<a href="https://file-examples.com/wp-content/uploads/2017/02/file-sample_100kB.doc" download="example.doc"> Download</a>');
}

function nuSelectBrowse(e) {
    if (e.target.download === undefined) {
		var r = $('#' + e.target.id).attr('data-nu-primary-key'); 	
		nuForm(nuGetProperty('form_id'), r);
	}
}
Janusz
nuBuilder Team
Posts: 508
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 10 times
Been thanked: 18 times

Re: Download file from browse form

Unread post by Janusz »

Hi Kev1n,
Thanks for reply.
So probably I need to rework the way I display/store the file in the browse table.

(I see - In your case you store/display link directly so it's enough just to click to have it open - In my case I store/display the "txt" file name only and afterwards when clicked I am dynamically creating the link which would like to open - but it was ok only when running such function from the button)

Janusz
If you like nuBuilder, please leave a review on SourceForge
Janusz
nuBuilder Team
Posts: 508
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 10 times
Been thanked: 18 times

Re: Download file from browse form

Unread post by Janusz »

>>> after some more check it appears that I had some typing mistake and used http instead of https - after correction it was OK.
If you like nuBuilder, please leave a review on SourceForge
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Download file from browse form

Unread post by kev1n »

Janusz wrote:>>> after some more check it appears that I had some typing mistake and used http instead of https - after correction it was OK.

:D
admin
Site Admin
Posts: 2822
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: Download file from browse form

Unread post by admin »

.
Tinka
Posts: 73
Joined: Mon Feb 24, 2014 2:58 pm

Re: Download file from browse form

Unread post by Tinka »

Do you have an example of using download directory outside the webroot on the server?
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Download file from browse form

Unread post by kev1n »

Can you elaborate a little bit on that and what you're trying to do?
Post Reply