所以我有這個表格,我提交給控制器
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
<div class="importStep">
<span class="importStepTitle">@ImportExport_Products.selectClassExport</span>
<div class="clear"></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 class="help-icon-container">
<span title="@ImportExport_Products.SelectLanguageForExport" class="helpIcon"></span>
</div>
</div>
<div class="clear"></div>
<button type="submit" id="submitExportBtn" class="btn btn-primary button btn-file r3 disabled">
@GlobalResource.ExportToExcel
</button>
</form>
控制器回傳一個檔案
{
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));
}
但是除非我轉到網路選項卡并在新選項卡中打開回應,否則下載不會開始。如何讓它自動下載?
uj5u.com熱心網友回復:
所以基本上我發現是因為我使用了ajax而發生了這個問題。簡單地改變:
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
到:
asp-action="GetGoodTagProperties"
asp-controller="DataMigration"
method="get"
id="exportForm">
做到了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/461027.html
上一篇:如何在動態生成的jquery串列中將值保存到localStorage,同時為每個元素生成一個href?
下一篇:導航到不同的控制器(和AJAX請求)時,會話變數會丟失,但是在Session_Start期間設定的會話變數是完整的
