Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error download file api to vue browser #2047

Open
danzf opened this issue Dec 18, 2024 · 0 comments
Open

error download file api to vue browser #2047

danzf opened this issue Dec 18, 2024 · 0 comments

Comments

@danzf
Copy link

danzf commented Dec 18, 2024

func (h *BookingMuseumHandler) DownloadExcels(c echo.Context) error {

data, _, err := h.bookingUcase.Fetch(ctx, au, &params)
if err != nil {
    return err
}

// Prepare the response data
pubRes := []domain.ListBookingMuseumDataResponse{}
for _, row := range data {
    res := domain.ListBookingMuseumDataResponse{
        ID:                row.ID,
        Nama:              row.PengunjungBookings.Nama,
        Email:             row.PengunjungBookings.Email,
        Kategori:          row.KategoriBookings.Nama,
        Sesi:              row.SesiBookings.Nama,
        JumlahPengunjung:  row.JumlahPengunjung,
        TanggalBerkunjung: row.TanggalBerkunjung,
        Dokumen:           row.DocBookings.Doc,
        BuktiBayarBookings: row.BuktiBayarBookings.Bukti,
        Status:            row.Status,
    }

    pubRes = append(pubRes, res)
}


// Create a new Excel file
f := excelize.NewFile()
index, err := f.NewSheet("Booking Museum")
if err != nil {
    return err
}
f.SetActiveSheet(index)
// Simpan file sementara di server untuk debugging

// Set header
headers := []string{
    "ID", "Nama", "Email", "Kategori", "Sesi", "Jumlah Pengunjung", "Tanggal Berkunjung", "Dokumen", "Bukti Bayar", "Status",
}
for col, header := range headers {
    f.SetCellValue("Booking Museum", fmt.Sprintf("%s1", string('A'+col)), header)
}

// Write data to Excel file
for i, res := range pubRes {
    f.SetCellValue("Booking Museum", fmt.Sprintf("A%d", i+2), strconv.FormatInt(res.ID, 10))
    f.SetCellValue("Booking Museum", fmt.Sprintf("B%d", i+2), res.Nama)
    f.SetCellValue("Booking Museum", fmt.Sprintf("C%d", i+2), res.Email)
    f.SetCellValue("Booking Museum", fmt.Sprintf("D%d", i+2), res.Kategori)
    f.SetCellValue("Booking Museum", fmt.Sprintf("E%d", i+2), res.Sesi)
    f.SetCellValue("Booking Museum", fmt.Sprintf("F%d", i+2), strconv.FormatInt(res.JumlahPengunjung, 10))
    f.SetCellValue("Booking Museum", fmt.Sprintf("G%d", i+2), res.TanggalBerkunjung)
    f.SetCellValue("Booking Museum", fmt.Sprintf("H%d", i+2), res.Dokumen)
    f.SetCellValue("Booking Museum", fmt.Sprintf("I%d", i+2), res.BuktiBayarBookings)
    f.SetCellValue("Booking Museum", fmt.Sprintf("J%d", i+2), res.Status)
}
if err := f.SaveAs("temp_booking_museum.xlsx"); err != nil {
	return err
}
buf := new(bytes.Buffer)
if err := f.Write(buf); err != nil {
	return c.JSON(500, "Failed to write Excel data to memory")
}

// Debugging: Periksa ukuran buffer
fmt.Println("Buffer size:", buf.Len())  // Pastikan ukuran file lebih besar dari 0
fmt.Printf("File size before sending: %d bytes\n", len(buf.Bytes()))

// Pastikan kita mengirimkan data dalam bentuk binary (Blob)
c.Response().Header().Set("Content-Disposition", "attachment; filename=booking_data.xlsx")
c.Response().Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
c.Response().Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("Transfer-Encoding", "chunked")

// Kirimkan data dalam bentuk byte array (Blob)
return c.Blob(200, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", buf.Bytes())
}

     echo/v4 v4.11.3
excelize/v2 v2.9.0 
hasil dari pemanggilan api 
Jepretan Layar 2024-12-18 pukul 12 17 07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant