CSV 仅返回列名,没有数据
CSV returning with column name only, no data
我正在尝试让我的插件从umbraco 中的博客post 导出内容,每个博客post 的contentType 都是'umbNewsItem'。因此,接下来我检查并测试了以下代码:
//This is my Controller
using ExportUmbracoBlogsPackage.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 ExportUmbracoBlogsPackage.App_Code
{
public class ExportAllBlogsController : UmbracoAuthorizedApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
public void ExportAll()
{
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = getPostList();
string attachment = "attachment; filename= BlogPosts.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");
HttpContext.Current.Response.CacheControl= "private";
WriteColumnName();
foreach (BlogPosts post in BlogPostList)
{
WritePostInfo(post);
}
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void AddComma(string value, StringBuilder sb)
{
sb.Append(',', ' ');
sb.Append(", ");
}
private void WriteColumnName()
{
string columnNames = "Content";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
public List<BlogPosts> getPostList()
{
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var select = new Sql("SELECT * FROM cmsPropertyData pd INNER JOIN cmsPropertyType pt ON pt.id = pd.propertytypeid INNER JOIN cmsContent c ON c.nodeId = pd.contentNodeId INNER JOIN cmsContentType ct ON ct.nodeId = c.contentType WHERE ct.alias = 'umbNewsItem' AND dataNtext IS NOT NULL;");
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = db.Fetch<BlogPosts>(select);
return BlogPostList;
}
}
}
基本上是下载csv,但是该列是空白的,没有任何值。为什么没有将值存储在列表中的任何建议?当我检查列表中的值是什么时,它给了我 9 个列表项,但所有项的值为 null。是什么导致了这种情况?
更新
没有抛出异常或错误,代码是 运行ning,但是它没有正确地从数据库中提取数据,我不知道为什么,当我 运行 相同在 SQL SERVER Management Studio 中查询查询工作正常。当我在我的程序中 运行 它时,查询返回行,因为列表返回 Count=9 的值并且在每个索引中存储模型中的变量 Content ,但它的值为 null我想不通。
提前致谢!
会不会是尝试写入数据的 StringBuilder 实际上从未附加任何内容?
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString()); // there's nothing in "sb" except commas at this point
HttpContext.Current.Response.Write(Environment.NewLine);
}
为什么要查询数据库而不是使用 ContentService?
并且您的 AddComma 函数没有将实际值添加到 StringBuilder 对象,可能就是这样。这是我的做法(AddComma 函数和 StringBuilder 的使用似乎有点不必要):
private void WritePostInfo(BlogPosts post)
{
HttpContext.Current.Response.Write(String.Format("{0}, ", post.Content));
HttpContext.Current.Response.Write(Environment.NewLine);
}
如果您想保留 AddComma 函数,它可能看起来像这样:
private void AddComma(string value, StringBuilder sb)
{
sb.AppendFormat("{0}, ", value);
}
话虽如此,我假设您稍后会添加更多列,对吗?否则整个逗号不是很有用 ;-)
我在 AddComma 方法中遗漏了一个 value.Replace
:
private void AddComma(string value, StringBuilder sb)
{
sb.Append(value.Replace(',', ' '));
sb.Append("; ");
}
加上这个,鲍勃你叔叔! sb 现在应该包含所有值。
我正在尝试让我的插件从umbraco 中的博客post 导出内容,每个博客post 的contentType 都是'umbNewsItem'。因此,接下来我检查并测试了以下代码:
//This is my Controller
using ExportUmbracoBlogsPackage.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 ExportUmbracoBlogsPackage.App_Code
{
public class ExportAllBlogsController : UmbracoAuthorizedApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
public void ExportAll()
{
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = getPostList();
string attachment = "attachment; filename= BlogPosts.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");
HttpContext.Current.Response.CacheControl= "private";
WriteColumnName();
foreach (BlogPosts post in BlogPostList)
{
WritePostInfo(post);
}
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void AddComma(string value, StringBuilder sb)
{
sb.Append(',', ' ');
sb.Append(", ");
}
private void WriteColumnName()
{
string columnNames = "Content";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
public List<BlogPosts> getPostList()
{
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var select = new Sql("SELECT * FROM cmsPropertyData pd INNER JOIN cmsPropertyType pt ON pt.id = pd.propertytypeid INNER JOIN cmsContent c ON c.nodeId = pd.contentNodeId INNER JOIN cmsContentType ct ON ct.nodeId = c.contentType WHERE ct.alias = 'umbNewsItem' AND dataNtext IS NOT NULL;");
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = db.Fetch<BlogPosts>(select);
return BlogPostList;
}
}
}
基本上是下载csv,但是该列是空白的,没有任何值。为什么没有将值存储在列表中的任何建议?当我检查列表中的值是什么时,它给了我 9 个列表项,但所有项的值为 null。是什么导致了这种情况?
更新
没有抛出异常或错误,代码是 运行ning,但是它没有正确地从数据库中提取数据,我不知道为什么,当我 运行 相同在 SQL SERVER Management Studio 中查询查询工作正常。当我在我的程序中 运行 它时,查询返回行,因为列表返回 Count=9 的值并且在每个索引中存储模型中的变量 Content ,但它的值为 null我想不通。
提前致谢!
会不会是尝试写入数据的 StringBuilder 实际上从未附加任何内容?
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString()); // there's nothing in "sb" except commas at this point
HttpContext.Current.Response.Write(Environment.NewLine);
}
为什么要查询数据库而不是使用 ContentService?
并且您的 AddComma 函数没有将实际值添加到 StringBuilder 对象,可能就是这样。这是我的做法(AddComma 函数和 StringBuilder 的使用似乎有点不必要):
private void WritePostInfo(BlogPosts post)
{
HttpContext.Current.Response.Write(String.Format("{0}, ", post.Content));
HttpContext.Current.Response.Write(Environment.NewLine);
}
如果您想保留 AddComma 函数,它可能看起来像这样:
private void AddComma(string value, StringBuilder sb)
{
sb.AppendFormat("{0}, ", value);
}
话虽如此,我假设您稍后会添加更多列,对吗?否则整个逗号不是很有用 ;-)
我在 AddComma 方法中遗漏了一个 value.Replace
:
private void AddComma(string value, StringBuilder sb)
{
sb.Append(value.Replace(',', ' '));
sb.Append("; ");
}
加上这个,鲍勃你叔叔! sb 现在应该包含所有值。