I have this handler which will download a sample excel file but on downloading the extension is automatically selected as 'All Files' but I just want to download .xlsx extension file without having to mention it in the file name. Is there any way to do it?
func (glh *GlobalHandler) getSampleFile_ExcelDbPopulation_of_CountriesStatesCitiesAreas(c *gin.Context) {
// Define the sample Excel file data with all columns
// '*' means columns are mandatory
sampleData := [][]string{
{"S No.", "*Country", "*State", "*City", "*Area", "*Pincode"},
{"1", "CountryA", "StateA", "CityA", "AreaA", "123456"},
}
// Create a new Excel file
file := excelize.NewFile()
// Set sheet name to "Sheet1"
sheetName := "Sheet1"
// Add rows to the sheet
for rowIndex, row := range sampleData {
for columnIndex, value := range row {
// Convert the column index to column name (e.g., 0 -> "A", 1 -> "B", etc.)
columnName, _ := excelize.ColumnNumberToName(columnIndex + 1)
cell := columnName + strconv.Itoa(rowIndex+1)
// Merge cells for the note row
if rowIndex == len(sampleData)-1 {
err := file.MergeCell(sheetName, "A6", "D6")
if err != nil {
gb_utils.LogError(glh.logger, "Error in getSampleFile_ExcelDbPopulation_of_CountriesStatesCitiesAreas: merging cells", err.Error(), 0, nil)
c.JSON(http.StatusInternalServerError, &gb_utils.GenericResponse{Message: gb_utils.ErrMsgOperationFailed("Sample File Download")})
return
}
// set the note
file.SetCellValue(sheetName, "A6", "Note: * fields are mandatory")
}
file.SetCellValue(sheetName, cell, value)
}
}
// Generate the Excel file contents
buffer := new(bytes.Buffer)
if err := file.Write(buffer); err != nil {
gb_utils.LogError(glh.logger, "Error in getSampleFile_ExcelDbPopulation_of_CountriesStatesCitiesAreas: writing Excel file", err.Error(), 0, nil)
c.JSON(http.StatusInternalServerError, &gb_utils.GenericResponse{Message: gb_utils.ErrMsgOperationFailed("Sample File Download")})
return
}
// Set the appropriate headers for Excel file download
downloadName := "SamplePopulationData.xlsx"
c.Header("Content-Description", "File Transfer")
c.Header("Content-Disposition", "attachment; filename="+downloadName)
// Write the Excel file to the response
c.Data(http.StatusOK, "application/octet-stream", buffer.Bytes())
}