HowTo Download big file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    HowTo Download big file

    Hello,
    browser security is restrictif ..
    so when you have a big download .. here solution :

    client/ detail


    PHP Code:
    define('custom:views/periode/detail', 'views/detail', function (Dep) {
    
    return Dep.extend({
    setup: function () {
    Dep.prototype.setup.call(this);
    this.addMenuItem('buttons', {
    name: 'rectif',
    label: 'rectif',
    style: 'default',
    action: 'rectif'
    }, true);
    },
    
    actionRectif: function () {
    $.ajax({
    url: 'Periode/action/rectif',
    type: 'POST',
    data: JSON.stringify({
    id: this.model.id
    }),
    }).done(function (result) {
    if (result['status'])
    {
    Espo.Ui.notify(false);
    Espo.Ui.notify( result['status'] , 'success', 2000);
    
    var byteString = atob(result.file.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    
    for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
    }
    
    window.location = URL.createObjectURL(new Blob([ab], { type: 'application/zip' }));
    
    }else{
    Espo.Ui.notify("Houston we have a problem!", 'danger', 2000);
    }
    }.bind(this));
    },
    afterRender: function () {
    Dep.prototype.afterRender.call(this);
    },
    
    });
    }); 
    
    backend / service :

    PHP Code:
    <?php
    
    namespace Espo\Custom\Services;
    
    use Espo\ORM\{
    Entity,
    EntityManager,
    ServiceFactory,
    };
    use \Sepa\CreditTransfer;
    use \ZipArchive;
    use \PhpOffice\PhpWord\TemplateProcessor;
    
    
    class Periode extends \Espo\Core\Templates\Services\Base
    {
    
    public function Rectif(Entity $entity, $id){
    
    $periodeId = $id;
    $em = $this->getEntityManager();
    $periode = $em->getEntity('Periode', $periodeId);
    $factureList = $em
    ->getRepository('Periode')
    ->getRelation($periode, 'factures')
    ->where([
    'facture.onError>' => 0
    ])
    ->find();
    
    $GLOBALS['log']->warning( count( $factureList ) );
    
    
    
    foreach($factureList as $facture)
    {
    $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('data/template/RectifTemplateNew.docx');
    $templateProcessor->setValue('periodeName', $periode->get('name'));
    $templateProcessor->setValue('conventionName', $facture->get('conventionName'));
    $templateProcessor->setValue('patientName', $facture->get('patientName'));
    $templateProcessor->setValue('nationalNumber', $facture->get('nationalNumber') );
    $templateProcessor->setValue('insuranceNumber', $facture->get('insuranceNumber'));
    $templateProcessor->setValue('insuranceLocalNumber', $facture->get('insuranceLocalNumber'));
    $templateProcessor->setValue('structured',$facture->get('structured') );
    
    $templateProcessor->setValue('onError',$facture->get('onError') );
    $templateProcessor->setValue('debit',$facture->get('debit') );
    $templateProcessor->setValue('credit',$facture->get('credit') );
    $templateProcessor->setValue('balance',$facture->get('balance') );
    
    $templateProcessor->setValue('description',$facture->get('description') );
    
    
    $careList = $em->getRepository('Facture')
    ->getRelation($facture, 'cares')
    ->where([
    'care.onError' => true,
    'care.hidden' => false,
    ])
    ->find();
    $nbCare = count($careList);
    $templateProcessor->cloneRow('careId', $nbCare);
    $i = 1;
    $amountByContact = [];
    foreach($careList as $care)
    {
    $templateProcessor->setValue('careId#'.$i, $care->get('dateTime') );
    $templateProcessor->setValue('careContact#'.$i, $care->get('contactName') );
    $templateProcessor->setValue('careAmount#'.$i, $care->get('amountInsurance') );
    $templateProcessor->setValue('careAmountCorrected#'.$i, $care->get('amountInsuranceCorrected') );
    $templateProcessor->setValue('careDescription#'.$i, $care->get('description') );
    $amountByContact[$care->get('contactName')] += $care->get('amountInsuranceCorrected');
    $i++;
    }
    
    
    $contactList = $em->getRepository('Facture')
    ->getRelation($facture, 'contacts')
    ->find();
    $nbContact = count($contactList);
    $templateProcessor->cloneRow('contactId', $nbContact);
    $i = 1;
    foreach($contactList as $contact)
    {
    
    $templateProcessor->setValue('contactId#'.$i, $contact->get('name') );
    $templateProcessor->setValue('inami#'.$i, $contact->get('inami') );
    $templateProcessor->setValue('amountContact#'.$i, $amountByContact[$contact->get('name')]);
    
    //$templateProcessor->setValue('contactId#'.$i, $contact->get('name') );
    $i++;
    }
    
    
    $templateProcessor->saveAs('data/tmp/' .'Rectif_Cario_' .$facture->get('name') .'.docx');
    }
    
    
    
    
    $rootPath = realpath('data/tmp');
    $tmp = 'data/delete/rectificatie.zip';
    $zip = new ZipArchive;
    
    $zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE);
    
    $files = new \RecursiveIteratorIterator(
    new \RecursiveDirectoryIterator($rootPath),
    \RecursiveIteratorIterator::LEAVES_ONLY
    );
    
    foreach ($files as $name => $file)
    {
    if (!$file->isDir())
    {
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);
    $zip->addFile($filePath, $relativePath);
    $filesToDelete[] = $filePath;
    }
    }
    
    $zip->close();
    foreach ($filesToDelete as $file)
    {
    unlink($file);
    }
    $content = file_get_contents($tmp);
    //sleep(1);
    unlink($tmp);
    
    header('Content-Description: File Transfer');
    header("Content-Type: application/force-download");
    header('Content-Type: application/zip');
    header('Content-type: "text/xml"; charset="utf8"');
    header('Content-Disposition: attachment; filename="rectificatie.zip"');
    header('Cache-Control: max-age=0');
    
    return [
    'status' => true,
    'message' => count( $factureList ),
    'file' => "data:application/zip;base64,".base64_encode($content)
    ];
    }
    }




    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Thanks for sharing item !

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      Just for reference, how big is big here?

      Is it above the PHP size limit config? 100MB? 1GB?

      Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #4
        Hello,
        mistake... it's more this :


        when server take time to respond to ajax call.. pop-up is blocked.
        so with detail.view function, it's not blocked.
        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment

        Working...