So i've got this form that I submit to a controller
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
<div >
<span >@ImportExport_Products.selectClassExport</span>
<div ></div>
<input id="exportType" name="exportType" type="hidden" value="0" />
@Html.DropDownListFor(m => selectedClassId, new SelectList(Model.importModel.Classes, "Key", "Value"), "---", new
{
id = "classesDropdownExport",
@class = "importStepCategorySelect r3 select2-lib"
})
@Html.DropDownListFor(m => languageId, new SelectList(Model.languages, "Id", "LanguageName"), new
{
id = "languagesDropDownExport",
@class = "importLanguageSelect r3 select2-lib"
})
<div >
<span title="@ImportExport_Products.SelectLanguageForExport" ></span>
</div>
</div>
<div ></div>
<button type="submit" id="submitExportBtn" >
@GlobalResource.ExportToExcel
</button>
</form>
The controller returns a file
{
if (selectedClassId <= 0)
{
throw new ArgumentException(nameof(selectedClassId));
}
if(languageId <= 0)
{
throw new ArgumentException(nameof(languageId));
}
const string exportTypeName = "GoodTagProperties";
Class catalogClass = await Class.GetAsync(this._dataAccessLayerAdapter, _memoryCache, selectedClassId, languageId, true);
if (string.IsNullOrEmpty(catalogClass.UniqueName))
{
return BadRequest("Incorrect class id");
}
IList<Property> properties = await GetAndInitializePropertiesForTable(selectedClassId, languageId);
DataTable dataTable = await PrepareTable(allGoods, languageId, catalogClass, properties);
return exportType == ExportType.Csv
? File(CSVHelper.ExportDataTableToCsvByteArray(dataTable), ExportHelper.CsvMimeType, ExportHelper.GetFileName(exportTypeName, selectedClassId.ToString()))
: File(ExportHelper.GetXlsDocumentInBytes(dataTable), ExportHelper.ExcelMimeType, ExportHelper.GetFileName(exportTypeName, selectedClassId.ToString(), ExportHelper.ExcelFileExtension));
}
but the download does not start unless I go to the network tab and open the response in a new tab. How do I make it to be automatically downloaded?
CodePudding user response:
So basically what I found was that this problem is occured because I've used ajax. Simply changing:
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
To:
asp-action="GetGoodTagProperties"
asp-controller="DataMigration"
method="get"
id="exportForm">
Did the trick