This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Download file real time
#1
Hi there,
I am doing a script which writes in a CSV value and name of a list of object selected by a tag.

Since the script is launched with a push button, I am wondering if It Is possibile download the file CSV directly on the browser. I mean such as trend log file push button export.

Thanks as Always.

Br.
Reply
#2
You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code:
$(function() {
  // additional class must be set to csv
  $('.csv').on('vclick', function() {
    window.open('/user/csv.lp');
  });
});

Use this as a starting point for csv.lp:
Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)
Reply
#3
Thank you Admin.

Can I do that in Mosaico as well?

Br
Reply
#4
Mosaic does not have custom js but maybe you could do something in the html widget.
------------------------------
Ctrl+F5
Reply
#5
(20.11.2020, 06:57)admin Wrote: You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code:
$(function() {
  // additional class must be set to csv
  $('.csv').on('vclick', function() {
    window.open('/user/csv.lp');
  });
});

Use this as a starting point for csv.lp:
Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)

Hi admin,
sorry but do not understand program language above. If I use <? LM inform me "Lua syntax error at line 1: unexpected symbol near '<'".

Thanks.
Reply
#6
.lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp
Reply
#7
(24.11.2020, 07:34)admin Wrote: .lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp

Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
value=grp.tag("consumption")

filename = string.format('consumption.csv', os.date('%d-%m-%Y-%H-%M'))

a={}

for index, id in ipairs(value) do
  name=id["name"]
 
  meter=id["data"]
 
  consumption=name .."," .. meter
 
  a[index]=consumi
  log(a)
end

csv = a
result, err = io.writefile (filename, csv)

-- output to the user
print(csv)

I generate file as above without success. Is it correct the procedure? Thanks.
Reply
#8
Try this:
Code:
<?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")

a={}

for index, id in ipairs(value) do
  name=id["name"]
  meter=id["data"]
  consumption=name .."," .. meter
  a[index]=consumi
end

print(a)
Reply
#9
(24.11.2020, 09:16)admin Wrote: Try this:
Code:
<?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")

a={}

for index, id in ipairs(value) do
  name=id["name"]
  meter=id["data"]
  consumption=name .."," .. meter
  a[index]=consumi
end

print(a)

Thank you very much Admin, but unfortunately when I launch the script on the visu I receive response from http://192.168.1.10/user/csv.lp that it is not possible reach the web site. The script is correct and I cannot identify the problem.

Thanks.
BR
Reply
#10
There were some errors in the script, this should work:
Code:
<?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')

csv = {}
objects = grp.tag('consumption')

for _, obj in ipairs(objects) do
  csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end

print(table.concat(csv, '\n'))
Reply
#11
(24.11.2020, 09:56)admin Wrote: There were some errors in the script, this should work:
Code:
<?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')

csv = {}
objects = grp.tag('consumption')

for _, obj in ipairs(objects) do
  csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end

print(table.concat(csv, '\n'))

FANTASTIC ADMIN!! THANK YOU VERY MUCH.

I would like only to clerify a thing. 

string.gsub(tostring(obj.value), ".", ",")

With this above I would like to replace dot with comma in the number. I do not know why but he replace all the number with ",". Hence 23.88 became ,,,,,

What is the reason?

Thanks.
Reply
#12
dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code:
value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.
Reply
#13
(24.11.2020, 10:33)admin Wrote: dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code:
value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.

Yes I know. I already use ";" in fact the format of the file in excel as well is perfect. Great Job. Thanks.

Now I work to download file from Mosaic as well.

Thanks.
Reply


Forum Jump: