Page 1 of 1

Download file from browse form

Posted: Sat Nov 16, 2019 7:19 am
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.

Re: Download file from browse form

Posted: Sat Nov 16, 2019 9:23 am
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);
	}
}

Re: Download file from browse form

Posted: Sat Nov 16, 2019 10:36 am
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

Re: Download file from browse form

Posted: Sat Nov 16, 2019 11:24 am
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.

Re: Download file from browse form

Posted: Tue Nov 19, 2019 6:11 pm
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

Re: Download file from browse form

Posted: Wed Nov 20, 2019 3:47 am
by admin
.

Re: Download file from browse form

Posted: Fri Mar 27, 2020 3:42 pm
by Tinka
Do you have an example of using download directory outside the webroot on the server?

Re: Download file from browse form

Posted: Tue Mar 31, 2020 9:34 am
by kev1n
Can you elaborate a little bit on that and what you're trying to do?