我试图用CSVHelper生成CSV文件并将其发送回浏览器,因此用户可以选择一个保存位置和文件名和保存数据。
I'm trying to use CSVHelper to generate a CSV file and send it back to a browser, so the user can select a save location and filename and save the data.
该网站是基于MVC。下面是jQuery的按钮code我使用拨打电话(数据的DTO列表中的一些序列化的Json重新presentation):
The website is MVC based. Here' the jQuery button code I'm using to make the call (data is some serialised Json representation of a DTO list):
$.ajax({
type: "POST",
url: unity.baseUrl + "common/ExportPayments",
data: data
});
下面是控制器code:
Here's the controller code:
[HttpPost]
public FileStreamResult ExportPayments()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
CsvWriter writer = new CsvWriter(sw);
List<Payment_dto> pd = _commonService.GetPayments();
foreach (var record in pd)
{
writer.WriteRecord(record);
}
sw.Flush();
return new FileStreamResult(ms, "text/csv");
}
这似乎达到precisely什么 - 调用方法步骤到code正确的位,但响应是空的,更何况是给用户提供一个文件对话框来保存数据。我已经通过这个code加大,而且带回的数据服务,将其写入,并抛出任何错误。所以我在做什么错了?
Which seems to achieve precisely nothing - invoking the method steps into the correct bit of code but the response is empty, let alone offering the user a file dialog to save the data. I've stepped through this code, and it brings back data from the service, writes it, and throws no errors. So what am I doing wrong?
编辑:回到这...
return File(ms.GetBuffer(), "text/csv", "export.csv");
...给我一个答复,其中包括了我期待的CSV格式的数据。但浏览器似乎仍然不知道该怎么做了它 - 没有下载选项提供给用户
... gives me a response, consisting of the csv-formatted data that I'm expecting. But the browser still doesn't seem to know what to do with it - no download option is offered to the user.
尝试下面code:
public FileStreamResult ExportPayments()
{
var result = WriteCsvToMemory(_commonService.GetPayments()());
var memoryStream = new MemoryStream(result);
return new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" };
}
public byte[] WriteCsvToMemory(IEnumerable<Payment_dto> records)
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var csvWriter = new CsvWriter(streamWriter))
{
csvWriter.WriteRecords(records);
streamWriter.Flush();
return memoryStream.ToArray();
}
}
更新
下面是如何通过一个复杂类型模型是使用 GET
HTTP方法的操作方法。我不preFER这种方法,它只是给你一个想法有实现这一目标的方法。
Below is how to pass a complex type model to an action method which is using GET
HTTP method. I don't prefer this approach, it just gives you an idea there is an approach to achieve this.
型号
public class Data
{
public int Id { get; set; }
public string Value { get; set; }
public static string Serialize(Data data)
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(data);
}
public static Data Deserialize(string data)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<Data>(data);
}
}
动作:
[HttpGet]
public FileStreamResult ExportPayments(string model)
{
//Deserialize model here
var result = WriteCsvToMemory(GetPayments());
var memoryStream = new MemoryStream(result);
return new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" };
}
查看:
@{
var data = new Data()
{
Id = 1,
Value = "This is test"
};
}
@Html.ActionLink("Export", "ExportPayments", new { model = Data.Serialize(data) })