我已经设置了单击按钮时要下载的CSV文件,我正在使用AngularJS和C#for Umbraco。我正在构建一个Umbraco插件,重点是导出所有字典键。我从数据库中选择它们并将它们放在一个类型为Item(从模型中拉出)的列表中。我遇到的问题是我没有收到任何错误,但浏览器没有下载CSV。
//ExportDictionaryAllController.cs
//This is where I am telling the browser to download the csv file.
using UmbracoImportExportPlugin.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Umbraco.Core.Persistence;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using System.Data;
using System.Data.SqlClient;
namespace UmbracoImportExportPlugin.App_Code
{
public class ExportDictionaryAllController : UmbracoAuthorizedApiController
{
[System.Web.Http.HttpPost]
public void ExportAll()
{
List<Item> DictionaryItems = new List<Item>();
DictionaryItems = getList();
string attachment = "attachment; filename= DictionaryItems.csv;";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
WriteColumnName();
foreach (Item item in DictionaryItems)
{
WriteItemInfo(item);
}
HttpContext.Current.Response.End();
}
private void WriteItemInfo(Item item)
{
StringBuilder sb = new StringBuilder();
AddComma(item.Key, sb);
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void AddComma(string value, StringBuilder sb)
{
sb.Append(value.Replace(',', ' '));
sb.Append(", ");
}
private void WriteColumnName()
{
string columnNames = "Key, English, Hebrew, Russian";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
public List<Item> getList()
{
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var select = new Sql("SELECT [key] FROM cmsDictionary;");
List<Item> DictionaryItems = new List<Item>();
DictionaryItems = db.Fetch<Item>(select);
return DictionaryItems;
}
}
}
任何人都可以向我解释为什么没有返回任何错误或异常,我也在使用Fiddler告诉我标题正在返回:
实体
Content-Disposition: attachment; filename= DictionaryItems.csv
Content-Type: text/csv; charset=utf-8
在Raw选项卡中,我看到以下内容:
HTTP/1.1 200 OK
Cache-Control: private
Pragma: public
Transfer-Encoding: chunked
Content-Type: text/csv; charset=utf-8
Server: Microsoft-IIS/10.0
Content-Disposition: attachment; filename= DictionaryItems.csv;
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcam9uYXRoYW4uYXZyYWhhbVxkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDEzXFByb2plY3RzXFVtYnJhY29JbXBvcnRFeHBvcnRQbHVnaW5cVW1icmFjb0ltcG9ydEV4cG9ydFBsdWdpblx1bWJyYWNvXGJhY2tvZmZpY2VcYXBpXEV4cG9ydERpY3Rpb25hcnlBbGxcRXhwb3J0QWxs?=
Date: Wed, 20 Apr 2016 12:13:45 GMT
94
Key, English, Hebrew, Russian
submit_button,
form_button,
hello_button,
world_button,
sublime_button,
bootstrap_button,
this_button,
0
答案 0 :(得分:0)
做类似的事情
[System.Web.Http.HttpGet]
public FileResult Download()
{
var csv = new List<string>();
csv.Add("Key, English, Hebrew, Russian");
//add items as string seperated by commas to csv
MemoryStream mstream = new MemoryStream();
StreamWriter sw = new StreamWriter(mstream);
foreach (var row in csv)
{
sw.Write(row);
}
sw.Flush();
mstream.Position = 0;
byte[] contents = new byte[mstream.Length];
mstream.Read(contents, 0, (int)mstream.Length);
return File(contents, "text/csv", "DictionaryItems.csv");
}
答案 1 :(得分:0)
我通过添加:
来修复我的angular-js控制器 window.open(downloadUrl, "_blank");
到角度控制器 - ExportAllController。
它应该没有必要,但它有效。