未从 Umbraco 下载 CSV 文件

CSV File not being downloaded from Umbraco

我已设置为在单击按钮时下载 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,它告诉我 header 正在返回:

实体

Content-Disposition: attachment; filename= DictionaryItems.csv
Content-Type: text/csv; charset=utf-8

在“原始”选项卡中,我看到以下内容:

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

做类似的事情

[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");
}

我用 angular-js 控制器修复了这个问题,方法是添加:

window.open(downloadUrl, "_blank");

到 angular 控制器 - ExportAllController。

它本不应该是必需的,但它起作用了。