控制器 returns 文件但下载未开始
Controller returns file but download does not start
所以我有这张表格要提交给管理员
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>
控制器returns一个文件
{
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));
}
但是除非我转到网络选项卡并在新选项卡中打开响应,否则下载不会开始。如何让它自动下载?
基本上我发现这个问题是因为我使用了ajax。简单地改变:
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
收件人:
asp-action="GetGoodTagProperties"
asp-controller="DataMigration"
method="get"
id="exportForm">
成功了
所以我有这张表格要提交给管理员
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>
控制器returns一个文件
{
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));
}
但是除非我转到网络选项卡并在新选项卡中打开响应,否则下载不会开始。如何让它自动下载?
基本上我发现这个问题是因为我使用了ajax。简单地改变:
asp-controller="DataMigration"
data-ajax="true"
data-ajax-method="GET"
id="exportForm">
收件人:
asp-action="GetGoodTagProperties"
asp-controller="DataMigration"
method="get"
id="exportForm">
成功了