Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Greetings!

The export csv helps create a single file. Everytime a new export is requested, the file is overwritten due to the truncate='1'.

Now the system I am working on, is a system where each user can request their own export. But since only one file is generated the file gets overwritten. I tried adding username to the file name but cms:write does not let that happen. It tends to take hardcoded value.

So can it be possible to:
1. Generate a new file everytime an export is requested which is userwise.
2. Save the file on the client machine rather than the server.
3. Dynamically generate a name for the exported file.
4. Download through ajax success.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
God evening!

Can anyone please shed some light on this!

Regards,
GXCPL (Priya)
Image
where innovation meets technology
Priya, try to make the question more specific. Here is a common guideline (copypasta from Stack):

- - -

The community is here to help you with specific coding, algorithm, or language problems.
Avoid asking opinion-based questions.

1) Summarize the problem

- Include details about your goal

- Describe expected and actual results

- Include any error messages

2) Describe what you’ve tried

Show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. You can get better answers when you provide research.

3) Show some code

When appropriate, share the minimum amount of code others need to reproduce your problem (also called a minimum, reproducible example)

- - -

Faithfully trying to help others understand your problem better, you will get help much faster! :)
@trendoman,
Thanks for the post you have shared from Stackoverflow!

Replying in accordance:
1) Summarize the problem

- Include details about your goal

- Describe expected and actual results

- Include any error messages

My Problem Summary: I am working on a system where each system user will generate a report which will be in the CSV format.

Goal Details and Expected and actual results:
The export csv helps create a single file. Everytime a new export is requested, the file is overwritten due to the truncate='1'.

Now the system I am working on, is a system where each user can request their own export. But since only one file is generated the file gets overwritten. I tried adding username to the file name but cms:write does not let that happen. It tends to take hardcoded value.

So can it be possible to:
1. Generate a new file everytime an export is requested which is userwise.
2. Save the file on the client machine rather than the server.
3. Dynamically generate a name for the exported file.
4. Download through ajax success

2) Describe what you’ve tried

I have tried the standard as in this post.

3) Show some code

I actually didnt do this coz a lot of times putting code does not help at all. Nevertheless I am sharing some now. This code is how I have created it according to my requirement, but has nothing to do with what I asked earlier. Its just implementation mehod. I hope this helps replicate the issue as mentioned.

I have a button to initiate an AJAX call:
Code: Select all
<button id="report" class="btn gxcpl-btn-outline-dark-primary gxcpl-button shadow"  type="button">
    <i class="fa fa-cloud-download"></i> Download
</button>


The AJAX:
Code: Select all
<script type="text/javascript">
            // Report AJAX
            $(function(){
                 $('#report').click(function(e){
                    e.preventDefault();
                     var elem = $(this);                    
                     $.ajax({
                     type: 'POST',
                     url: "<cms:show k_site_link />support-files/ajax-generate-report.php",
                     data:  {
                        report_from_date   : "<cms:gpc 'report_from_date' />",
                        report_to_date      : "<cms:gpc 'report_to_date' />",
                        report_status      : "<cms:gpc 'report_status' />",
                        report_owner      : "<cms:pages masterpage=k_user_template id=k_user_id limit='1'><cms:show k_user_name /></cms:pages>",
                        export            : "1"
                     },
                     success: function(file) {
                          // if  (file != '') {
                          //    window.open(file);
                          // }
                      }
                  });
                  return false;
                  });
              });
              // Report AJAX
         </script>


The CSV Exporter Code:
Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
<cms:template title="AJAX - Report Generation" order="10" />

<cms:set save_report_from_date="<cms:gpc 'report_from_date' method='post' />" scope="global" />
<cms:set save_report_to_date="<cms:gpc 'report_to_date' method='post' />" scope="global" />
<cms:set save_report_status="<cms:gpc 'report_status' method='post' />" scope="global" />
<cms:set save_report_owner="<cms:gpc 'report_owner' method='post' />" scope="global" />
<cms:set save_export="<cms:gpc 'export' method='post' />" scope="global" />

<cms:if k_logged_out>
    <cms:redirect url="<cms:show k_login_link />" />
</cms:if>
<cms:if save_report_status eq '-'>
    <cms:set show_save_report_status = " | ctr_indate>=<cms:show save_report_from_date /> | ctr_outdate <= <cms:show save_report_to_date />" scope="global" />
<cms:else />
    <cms:set show_save_report_status = " | ctr_indate>=<cms:show save_report_from_date /> | ctr_outdate <= <cms:show save_report_to_date /> | ctr_status == <cms:show save_report_status />" scope="global" />
</cms:if>

<cms:if save_export eq "1">
    <cms:pages masterpage="index.php" paginate="1" limit="100" show_future_entries="1" order="asc" custom_field="ctr_owner=<cms:show save_report_owner /><cms:show show_save_report_status />" >
        <cms:if k_paginated_top >
            <cms:if k_current_page='1'>
                <cms:write 'report.csv' add_newline='1' truncate='1'>Sr.No.,Name,Mobile,Status</cms:write>
            </cms:if>           
            <cms:if k_paginate_link_next >
                <script language="JavaScript" type="text/javascript">
                    var myVar;
                    myVar = window.setTimeout( 'location.href="<cms:show k_paginate_link_next />";', 100 );
                </script>
                <button onclick="clearTimeout(myVar);">Stop</button>
            <cms:else />
                <cms:set write_footer='1' 'global'/>
                Done!   
            </cms:if>           
            <h3><cms:show k_current_page /> / <cms:show k_total_pages /> pages (Total <cms:show k_total_records /> records. Showing <cms:show k_paginate_limit /> records per page)</h3>
        </cms:if>
            <h3><cms:show k_current_record /></h3>
            <cms:write 'report.csv' add_newline='1'><cms:format_csv k_absolute_count/>,<cms:format_csv ctr_name/>,<cms:format_csv ctr_mobile/>,<cms:format_csv ctr_status/></cms:write>
        <cms:if k_paginated_bottom >
            <cms:if write_footer>
            <cms:else />
                <cms:paginator simple='1' />
            </cms:if>   
        </cms:if>
    </cms:pages>
     
</cms:if>
<?php COUCH::invoke(); ?>


The questions still remain the same as asked earlier. I hope this provides more details as expected. Please let me know if more detail is needed. I would be willing to share if something is needed in specific.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
Thanks) Re: dynamic file name, amend the coding of tag cms:write (everywhere it is used) as follows to solve the naming issue -

<cms:write "report_<cms:show save_report_owner />.csv" add_newline='1' truncate='1'>..</cms:write>
Re: unique file name, try to add some unique id to the name, the simplest being current date in any suitable format —

"report_<cms:show save_report_owner />_<cms:date format='U'/>.csv"
"report_<cms:show save_report_owner />_<cms:date format='YmdHis'/>.csv"
Re: downloading report via ajax

There are 2 options:

1) send a link to report via ajax (see tags » cloak_url to generate a link) then
‎~ ~ ~ a) either shop popup with the link, so the user clicks it himself or
~ ~ ~ b) create a HTML <a> element and trigger a click on it via JS. Following listing has a good example of ajax code for this b) suggestion: Download-File-in-AJAX-Response-Success-using-jQuery
EDIT: May also redirect user to the download page with the link as in this thread viewtopic.php?f=4&t=10687&start=10#p26986

OR,

2) send actual report content in response and convert those bytes on client-side to a file. The suggested listing from the previous section actually implements it in fullest. I suggest to consider a FileSaver.js, StreamSaver.js libraries that help with this. There are probably (I don't know) a few similar libs, choose one that fits your case best.

Study this SO answer: download-a-file-by-jquery-ajax. If in the process you need to have report sent as Base64 encoded string, feel free to use the tag <cms:base64_encode> from Tweakus Tags » base64_encode repo, which is a comfortable Couch tag wrapping the PHP's function base64_encode.
@trendoman

Sorry for the delay in reply.

Thanks for the suggestion. It worked out well. Just that In place of the ajax download suggested by you, since I am to use mobile download through a web view application, i did it by sending the cloak url directly as a URI to intent.

Thanks for the help.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
Glad it helped :)
The cloaked link solution makes it the variant 1) a), right?
genxcoders wrote: @trendoman

Sorry for the delay in reply.

Thanks for the suggestion. It worked out well. Just that In place of the ajax download suggested by you, since I am to use mobile download through a web view application, i did it by sending the cloak url directly as a URI to intent.

Thanks for the help.

Regards,
GXCPL (Priya)
@trendoman,

The cloaked link solution makes it the variant 1) a), right?

Yes it does. The only thing is that the popup has to be coded as an intent in at the android end rather than the web end.

Thanks once again.

I have one more question, but I will start a new thread for it!

Regards,
GXCPL (Priya)
Image
where innovation meets technology
10 posts Page 1 of 1
cron