หน้า 1 จากทั้งหมด 1

กด download แล้วไม่ไปหน้าที่เราจะทำ download ไฟล์

โพสต์แล้ว: 13/01/2021 7:13 pm
โดย eange08
ทำกดปุ่ม download แล้วให้เด้งไปที่ไฟล์ exportimg_crop.php แต่พอกดเด้งไปแล้วไม่ยอมไปที่ไฟล์ exportimg_crop.php ต้องทำอย่างไรค่ะ
มันเด้งหน้าใหม่แต่ขึ้น Object not found! ไม่ไปหน้า exportimg_crop.php
Screenshot from 2021-01-13 19-10-20.png
ไฟล์ script.js

โค้ด: เลือกทั้งหมด

   $(document).on('click','.btn_downcrop',function () {  //----คลิ๊กมาจากปุ่ม download 
        var input=$(this).closest('.row').find('.cropbe4 input');
        var data = {};
        $.each(input, function() {
            var name=String($(this).attr('name')).replace('[]','');
            data[name]=$(this).val();
            
        });
       //-------ส่วนที่จะเด้งไปหน้าสำหรับทำตัว download ไฟล์
          $.post('exportimg_crop.php', {'file':'123'}, function (result) {                  
                window.open(result.id, '_blank');
            });

    });
ไฟล์ exportimg_crop.php

โค้ด: เลือกทั้งหมด

<?php
header('Content-type: application/json');
echo '{"status":"success","id":"222"}';   
exit;
?>

Re: กด download แล้วไม่ไปหน้าที่เราจะทำ download ไฟล์

โพสต์แล้ว: 14/01/2021 4:53 pm
โดย eange08
ทำได้แล้วนะคะ โดยทำ
1. ในไฟล์ script.js

โค้ด: เลือกทั้งหมด

 $(document).on('click','.btn_downcrop',function () { 
        var input=$(this).closest('.row').find('.cropbe4 input');
        var data = {};
        $.each(input, function() {
            var name=String($(this).attr('name')).replace('[]','');
            data[name]=$(this).val();
            
        });          

          $.post('exportimg_crop.php', data, function (result) {                  //-------ส่วนเข้าไปแปลงภาพด้วย imagick แล้วส่งค่าที่จะไปที่ไฟล์ download ใน result
                console.log(data); 
              window.open('exportimg.php?id='+result.file_id+'&type='+result.file_type, '_blank'); //-----เมื่อทำการแปลงเสร็จก็ส่งค่ารูปไปที่ไฟล์ exportimg.php
            });

    });
2. ไฟล์ exportimg_crop.php สำหรับแปลงรูปด้วย imagick

โค้ด: เลือกทั้งหมด

<?php
date_default_timezone_set("Asia/Bangkok");
require_once'conn.php';
//------ส่วนที่เรียกค่าจาก data ที่ส่งค่า post มา
if(!empty($_POST['id_img'])){
    $id_img=$_POST['id_img'];
}else{
    $id_img='';
}
if(!empty($_POST['x'])){
    $img_x=$_POST['x'];
}else{
    $img_x='';
}
.............

//-----ส่งค่ากลับไปที่ script.js แล้วเพื่อเอาไปใช้ทำ download ไฟล์ที่ exportimg.php 
header('Content-type: application/json');
echo '{"status":"success", "file_id": "'.$straft_filenm.'","file_type":"'.$straft_mime.'"}';   
exit;
?>

3. ไฟล์ exportimg.php สำหรับดึงภาพเพื่อ download

โค้ด: เลือกทั้งหมด

<?php
//----รับค่าจาก url ที่ส่งมาจาก script.js
if(!empty($_REQUEST['id'])){
    $str_id=$_REQUEST['id'];
}else{
    $str_id='';
}
if(!empty($_REQUEST['type'])){
    $str_type=$_REQUEST['type'];
}else{
    $str_type='';
}  
//-----ส่วนดึงภาพแล้ว download ไฟล์ออกไป
$path = dirname(__FILE__) . '/uploads/after/'.$str_id;
if (file_exists($path)) {
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: ".$str_type);
 header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path));
echo file_get_contents($path);
}else{
    echo "<script>alert('ไม่พบไฟล์ที่ต้องการ download ค่ะ');</script>";
}
exit;
?>