我正在 MVC 中实现导出到 csv。
由于参数是日期时间类型,由于用户文化不同,我需要在帖子中发送数据。
服务器代码:
[HttpPost]
public async Task<FileResult> ExportBomTotal(ReportRequestVM reportRequestVM)
{
List<MyData> result = await service.getData(reportRequestVM);
string fileName = string.Format("file{0}-{1} .csv", reportRequestVM.StartDate.ToShortDateString(), reportRequestVM.EndDate.ToShortDateString());
return new CSVResult<MyData>(result) { FileDownloadName = fileName };
}
在客户端,我试图像这样在帖子中发送数据:
$.ajax({
url: '@Url.Action("ExportBomTotal", "Analytics")',
type: 'POST',
data: JSON.stringify(searchParameters),
contentType: 'application/json',
success: function (data) {
debugger;
// window.location.assign(data);
// window.location.replace(data);
},
error: function (response) {
debugger;
},
cache: false
});
日期被传递到服务器,到目前为止这很好,但我无法获得下载文件弹出窗口。
我想我需要使用 window.location 但请求必须是后期操作。
对于那些如何要求 CSVResult 公共密封类 CSVResult : FileResult where T : class { private readonly IEnumerable _collection;
public CSVResult(IEnumerable<T> collection)
: base("text/csv")
{
_collection = collection;
}
protected override void WriteFile(HttpResponseBase response)
{
Stream outputStream = response.OutputStream;
using (MemoryStream mstream = new MemoryStream())
{
WriteObject(mstream);
outputStream.Write(mstream.GetBuffer(), 0, (int)mstream.Length);
}
}
private void WriteObject(Stream stream)
{
// We will follow the recommandations stated in this article
// http://www.commentcamarche.net/faq/sujet-7273-exporter-a-coup-sur-du-csv
StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.Default);
//modelType.
Dictionary<string, bool> dict = new Dictionary<string, bool>();
// Render columns
Type modelType = typeof(T);
PropertyInfo[] props = modelType.GetProperties();
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (VisibleAttribute visible in attrs.OfType<VisibleAttribute>())
{
dict.Add(prop.Name, visible.Hide);
}
}
List<ModelMetadata> metadatas = ModelMetadataProviders.Current.GetMetadataForProperties(null, modelType)
.Where(m => !dict.ContainsKey(m.PropertyName) || dict[m.PropertyName] )
.OrderBy(p => p.Order).ToList();
foreach (ModelMetadata t in metadatas)
{
WriteValue(writer, t.DisplayName ?? t.PropertyName);
}
writer.WriteLine();
// Render data
var en = _collection.GetEnumerator();
while (en.MoveNext())
{
ModelMetadata mprop = ModelMetadataProviders.Current.GetMetadataForType(() => en.Current, modelType);
var allowedProperties = mprop.Properties
.Where(m => !dict.ContainsKey(m.PropertyName) || dict[m.PropertyName]);
foreach (ModelMetadata prop in allowedProperties)
{
WriteValue(writer, prop.SimpleDisplayText ?? String.Empty);
}
writer.WriteLine();
}
writer.Flush();
}
/// <summary>
/// Writes the value.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="literal">The literal.</param>
private static void WriteValue(StreamWriter writer, String literal)
{
// Enclose values in quote
writer.Write("\"");
string line = literal;//.Replace("\"", "\"\"");
writer.Write(line);
writer.Write("\",");
}
}
任何建议将不胜感激,
1o,
罗尼