Javascript - Download filename different from the original (want equal)

Asked

Viewed 43 times

-2

Good afternoon, gentlemen, all right? I have a file I Gero on. XLS in php and ask to download in javascript, IE, I file in php and it saves the file in the folder and return the name of this file to javascript and it just downloads.

It works, download it right, however, the name of the file when it is saved in the folder is: report.xls

When I go down in javascript it gets: _ __ _ __ _report.xls

these characters appear before the name. I’ve put TRIM everywhere, because I don’t know if this can be caused by space, but it hasn’t solved.

my javascript ta so.

$.post({
            url: "handle.php", // the resource where youre request will go throw
            type: "POST", // HTTP verb
            data: {action: 'baixar_relatorio', tipo: $('#tipo').val(), status: $('#status').val(), carregamento: $('#carregamento').val(),
                datade: $('#datade').val(), dataate: $('#dataate').val(), placa: $('#placa').val(), motorista: $('#motorista').val()
            },
            success: function (response) {
                var a = document.createElement('a');
                a.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(response.trim()));
                a.href ='<?php echo  $_SESSION['URL'] ?>'+'/file/relatorio/logistic/'+response.trim();
                a.download = response;
                document.body.append(a);
                a.click();
                a.remove();

            }
        });

You are downloading right, only the name that is weird, full of stuff before. In the folder where the file is downloaded, is with the right name.

From now on I thank you for your attention.

EDITED: my back-end I doubt it is, because I told you, the file saves with the right name.... ta ai ai

<?php

include $_SERVER['DOCUMENT_ROOT']. '/Pdo/Pdo-sqlserver.php'; include $_SERVER['DOCUMENT_ROOT']. '/Phpmailer/src/Phpmailer.php'; include $_SERVER['DOCUMENT_ROOT']. '/Phpmailer/src/SMTP.php'; include $_SERVER['DOCUMENT_ROOT']. '/Phpmailer/src/Exception.php'; include $_SERVER['DOCUMENT_ROOT']. '/Phpmailer/src/Oauth.php'; include $_SERVER['DOCUMENT_ROOT']. '/Serasa/consulta_serasa.php'; include $_SERVER['DOCUMENT_ROOT']. '/cdc/consulta_cdc.php'; include $_SERVER['DOCUMENT_ROOT']. '/script/mail.php';

session_start();

// DOWNLOAD EXCEL PHP

if ($_POST['action'] == 'download_report') {

/** Include Phpexcel */ require_once $_SESSION['ROOT']. '/Phpexcel/Phpexcel.php'; require_once $_SESSION['ROOT']. '/Phpexcel/Phpexcel/Cell.php'; require_once $_SESSION['ROOT']. '/Phpexcel/Phpexcel/Worksheet.php'; require_once $_SESSION['ROOT']. '/Phpexcel/Phpexcel/Style.php';

$file_name = 'relatorio-'.date('Y-m-d_His').'.xls';

$Conexao = ConexaoSQLServer_Quality::getConnection();

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
//$objPHPExcel->setActiveSheetIndex(0);

$sharedStyle1 = new PHPExcel_Style();
$sharedStyle3 = new PHPExcel_Style();
$sharedStyle4 = new PHPExcel_Style();
$sharedStyle5 = new PHPExcel_Style();
$sharedStyle6 = new PHPExcel_Style();

$sharedStyle1->applyFromArray(
array('fill' => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '245c7b') // azul escuro
            ),
        'font' => array(
            'bold' => true,
            'color' => array('rgb' => 'ffffff')
            )
    ));

$sharedStyle3->applyFromArray(
array('fill' => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'e9f2fc') // azul claro
            ),
        'font' => array(
            'bold' => true,
            ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
         )
));

$sharedStyle4->applyFromArray(
array('fill' => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'fff7e6') // rosa claro
            ),
        'font' => array(
            'bold' => true,
            ),
         'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
         )
));



$sharedStyle5->applyFromArray(
array('fill' => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '78afed') // azul escuro
            ),
        'font' => array(
            'bold' => true,
            'color' => array('rgb' => 'ffffff'),
            ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
         )
));

$sharedStyle6->applyFromArray(
array('fill' => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'ffcc66') // rosa escuro
            ),
        'font' => array(
            'bold' => true,
            'color' => array('rgb' => 'ffffff'),
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
        )
));


    $array_status_pai = array(
        'A' => 'Aguardando',
        'Z' => 'Finalizado',
        'O' => 'Ordem Aceita',
        'B' => 'Em Andamento',
        'F' => 'Cancelado',
    );

    $array_status_filho = array(
        'A' => 'Chegou ao patio',
        'B' => 'Entrada',
        'G' => 'Aguardando OP',
        'C' => 'Carga Autorizada',
        'D' => 'Saida',
        'Z' => 'Finalizado',
    );


    $where = null;
    if($_POST['datade'] != null and $_POST['dataate'] != null){
            $d1 = substr($_POST['datade'], 0, 2);
            $m1 = substr($_POST['datade'], 3, 2);
            $a1 = substr($_POST['datade'], 6, 4);
            $data1 = $a1.'-'.$m1.'-'.$d1;

            $d2 = substr($_POST['dataate'], 0, 2);
            $m2 = substr($_POST['dataate'], 3, 2);
            $a2 = substr($_POST['dataate'], 6, 4);
            $data2 = $a2.'-'.$m2.'-'.$d2;
        ($where == null) ? $where .= " WHERE O_DATA_CRIACAO BETWEEN '".$data1."' AND '".$data2."' " : $where .= " AND O_DATA_CRIACAO BETWEEN '".$data1."' AND '".$data2."' ";

    }else if($_POST['datade'] != null and $_POST['dataate'] == null){
        $d1 = substr($_POST['datade'], 0, 2);
        $m1 = substr($_POST['datade'], 3, 2);
        $a1 = substr($_POST['datade'], 6, 4);
        $data1 = $a1.'-'.$m1.'-'.$d1;

        ($where == null) ? $where .= " WHERE O_DATA_CRIACAO >= '".$data1."' " : $where .= " AND O_DATA_CRIACAO >= '".$data1."' ";
    }

    if(isset($_POST['status']) and $_POST['status'] != null){
        $w = null;
        if(!in_array("1", $_POST['status'])){
            foreach($_POST['status'] as $s){
                $w .= "'".$s."',";
            }
            ($where == null) ? $where .= " WHERE O_STATUS IN (".substr($w, 0, -1).") " : $where .= " AND O_STATUS IN (".substr($w, 0, -1).") ";
        }
    }
if($_POST['tipo'] == 2){

        if(isset($_POST['motorista']) and $_POST['motorista'] != null){
            $w = null;
            foreach($_POST['motorista'] as $m){
                $w .= "'".$m."',";
            }
            ($where == null) ? $where .= " WHERE OCT_MOTORISTA_CPF IN (".substr($w, 0, -1).") " : $where .= " AND OCT_MOTORISTA_CPF IN (".substr($w, 0, -1).") ";
        }

        if(isset($_POST['placa']) and $_POST['placa'] != null){
            ($where == null) ? $where .= " WHERE OCT_PLACA_CAVALO = '".$_POST['placa']."' " : $where .= " AND OCT_PLACA_CAVALO = '".$_POST['placa']."' ";
        }

    // Rename worksheet
    $objPHPExcel->getActiveSheet(0)->setTitle('OutBound');
    // cabeçalho da planilha
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Outbound.')
            ->setCellValue('A2', 'ID')
            ->setCellValue('B2', 'Pedido Protheus')
            ->setCellValue('C2', 'Pedido Cliente')
            ->setCellValue('D2', 'Cliente')
            ->setCellValue('E2', 'Total Programado')
            ->setCellValue('F2', 'Solicitação')
            ->setCellValue('G2', 'Produção')
            ->setCellValue('H2', 'Entrega')
            ->setCellValue('I2', 'Previsão')
            ->setCellValue('J2', 'Status da OC')
            ->setCellValue('K2', 'Transportadora')
            ->setCellValue('L2', 'Motorista')
            ->setCellValue('M2', 'Volume')
            ->setCellValue('N2', 'Placa Cavalo')
            ->setCellValue('O2', 'Placa Carreta')
            ->setCellValue('P2', 'Status')
            ->setCellValue('Q2', 'Chegada')
            ->setCellValue('R2', 'Entrada')
            ->setCellValue('S2', 'Carregamento')
            ->setCellValue('T2', 'Saida')
            ->setCellValue('U2', 'Cancelado');


    // retornar produto acabado
    $array_out = array();
    $array_in = array();
    $linha = 2;

    $query = $Conexao->query("SELECT O_ID, O_TIPO, O_PEDIDO_PRO, O_PEDIDO_CLI, O_CLIENTE, O_TELEFONE, 
        convert(varchar, O_DATA_SOLICITA, 103) + ' ' + convert(varchar, O_DATA_SOLICITA, 8) O_DATA_SOLICITA, 
        convert(varchar, O_DATA_PROD, 103) + ' ' + convert(varchar, O_DATA_PROD, 8) O_DATA_PROD, 
        convert(varchar, O_DATA_ENTREGA, 103) + ' ' + convert(varchar, O_DATA_ENTREGA, 8) O_DATA_ENTREGA, 
        convert(varchar, O_DATA_PREVISAO, 103) + ' ' + convert(varchar, O_DATA_PREVISAO, 8) O_DATA_PREVISAO, 
        O_COD_PROD, O_FORMULA, O_TOTAL_PROG, O_SALDO, O_UNIDADE, O_CIDADE, O_SOLICITANTE, O_URGENCIA, O_GRUPO_CLI, O_OBS, 
        O_DETALHES, O_LOCAL_COLETA, O_STATUS, O_TRANSPORTADORA, OCT_ID, OCT_VOLUME, OCT_PLACA_CAVALO, OCT_PLACA_CARRETA, OCT_MOTORISTA_CPF, 
        OCT_MOTORISTA_NOME, 
        convert(varchar, OCT_DATA_CHEGADA, 103) + ' ' + convert(varchar, OCT_DATA_CHEGADA, 8) OCT_DATA_CHEGADA, OCT_TELEFONE_MOT, OCT_STATUS, 
        convert(varchar, OCT_DATA_ENTRADA, 103) + ' ' + convert(varchar, OCT_DATA_ENTRADA, 8) OCT_DATA_ENTRADA, 
        convert(varchar, OCT_DATA_CARGA, 103) + ' ' + convert(varchar, OCT_DATA_CARGA, 8) OCT_DATA_CARGA, 
        convert(varchar, OCT_DATA_SAIDA, 103) + ' ' + convert(varchar, OCT_DATA_SAIDA, 8) OCT_DATA_SAIDA, 
        convert(varchar, OCT_DATA_CANCELADO, 103) + ' ' + convert(varchar, OCT_DATA_CANCELADO, 8) OCT_DATA_CANCELADO, 
        convert(varchar, OCT_DATA_OP, 103) + ' ' + convert(varchar, OCT_DATA_OP, 8) OCT_DATA_OP, OCT_PERTENCE, O_QTD_CARGA, O_END_COLETA, O_CADENCIA, 
        O_TARIFA, O_DESTINO, O_END_DESTINO, O_PEDIDO_MIC, 
        O_IMPORTACAO, O_DI, O_NAVIO, O_QTD, O_FRETE, 
        convert(varchar, O_DATA_LIBERACAO, 103) + ' ' + convert(varchar, O_DATA_LIBERACAO, 8) O_DATA_LIBERACAO, 
        convert(varchar, O_DATA_VALIDADE, 103) + ' ' + convert(varchar, O_DATA_VALIDADE, 8) O_DATA_VALIDADE, 
        convert(varchar, O_DATA_CRIACAO, 103) + ' ' + convert(varchar, O_DATA_CRIACAO, 8) O_DATA_CRIACAO, O_PORTO, O_JANELA, O_BERCO, O_CARGA_CARREGADA, USU_NOME
        FROM ORDEM_CARREGAMENTO 
        LEFT JOIN ORDEM_CARREGAMENTO_T  ON OCT_ORDEM_ID = O_ID 
        LEFT JOIN USUARIO_LOGIN ON USU_ID = O_TRANSPORTADORA  ".$where." ");
    while ($row = $query->fetch()) {
        if($row['O_TIPO'] == 'out'){
            $array_out[] = $row;
        }else if($row['O_TIPO'] == 'in'){
            $array_in[] = $row[]
        }
    }

    foreach($array_out as $out){

        $linha++;
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(0, $linha, $out['O_ID']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(1, $linha, $out['O_PEDIDO_PRO']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(2, $linha, $out['O_PEDIDO_CLI']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(3, $linha, $out['O_CLIENTE']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(4, $linha, $out['O_TOTAL_PROG']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(5, $linha, $out['O_DATA_SOLICITA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(6, $linha, $out['O_DATA_PROD']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(7, $linha, $out['O_DATA_ENTREGA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(8, $linha, $out['O_DATA_PREVISAO']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(9, $linha, $array_status_pai[$out['O_STATUS']]);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(10, $linha, $out['USU_NOME']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(11, $linha, $out['OCT_MOTORISTA_NOME']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(12, $linha, $out['OCT_VOLUME']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(13, $linha, $out['OCT_PLACA_CAVALO']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(14, $linha, $out['OCT_PLACA_CARRETA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(15, $linha, ($out['OCT_STATUS'] != NULL) ? $array_status_filho[$out['OCT_STATUS']] : '');
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(16, $linha, $out['OCT_DATA_CHEGADA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(17, $linha, $out['OCT_DATA_ENTRADA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(18, $linha, $out['OCT_DATA_CARGA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(19, $linha, $out['OCT_DATA_SAIDA']);
        $objPHPExcel->getActiveSheet(0)->setCellValueByColumnAndRow(20, $linha, $out['OCT_DATA_CANCELADO']);
    }

    //$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
    $objPHPExcel->getActiveSheet(0)->setSharedStyle($sharedStyle1, "A1:U2");

    $objPHPExcel->getActiveSheet(0)->getColumnDimension('A')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('B')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('C')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('D')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('E')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('F')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('G')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('H')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('I')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('J')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('K')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('L')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('M')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('N')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('O')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('P')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('Q')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('R')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('S')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('T')->setAutoSize(true);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('U')->setAutoSize(true);


    // Rename worksheet
    $objPHPExcel->getActiveSheet(0)->setTitle('InBound');
    // cabeçalho da planilha
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'InBound.')
            ->setCellValue('A2', 'ID')
            ->setCellValue('B2', 'Produto')
            ->setCellValue('C2', 'DI')
            ->setCellValue('D2', 'Coleta')
            ->setCellValue('E2', 'Total Programado')
            ->setCellValue('F2', 'Solicitação')
            ->setCellValue('G2', 'Produção')
            ->setCellValue('H2', 'Entrega')
            ->setCellValue('I2', 'Previsão')
            ->setCellValue('J2', 'Status da OC')
            ->setCellValue('K2', 'Transportadora')
            ->setCellValue('L2', 'Motorista')
            ->setCellValue('M2', 'Volume')
            ->setCellValue('N2', 'Placa Cavalo')
            ->setCellValue('O2', 'Placa Carreta')
            ->setCellValue('P2', 'Status')
            ->setCellValue('Q2', 'Chegada')
            ->setCellValue('R2', 'Entrada')
            ->setCellValue('S2', 'Carregamento')
            ->setCellValue('T2', 'Saida')
            ->setCellValue('U2', 'Cancelado');


    }   
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save($_SESSION['ROOT'].'/file/relatorio/logistic/'.$file_name);


    echo trim($file_name);
}
  • Why did you do this a.setAttribute('href',... and right after this a.href ='<?php echo ..., there is no reason, the first nor will be processed, you this overwriting anyway. We have no way to know relatorio.xls turned _ __ _ __ _relatorio.xls, must be a bug in your back-end (in your Handle.php), Trim will not fix this.

1 answer

0


This is the solution to the problem...

a.download = response.trim();

You have to put Trim in the return of javascript.

For the ignorant on duty, it was worth denying.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.