将byte [] - Array转换为ANSI

时间:2015-08-31 08:46:41

标签: c# sql-server unicode visual-studio-lightswitch ansi

我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte [] blob转换为Windows-1252 ANSI格式,并将结果返回到文件下载。

这是我认为应该起作用的......

我正在用

创建字符串
int v_fileId = Int32.Parse(context.Request.QueryString["p_FileId"]);

DataTableName v_fexpd = serverContext.DataWorkspace.ApplicationData.DataTableName_SingleOrDefault(v_fileId);
MemoryStream memStream = new MemoryStream(v_fexpd.Inhalt);

string v_Content= System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content);

context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Filename);
context.Response.Write( v_Content );
context.Response.End();

并将其写入数据库。当我尝试使用

进行检索时
<b>([^<]+)<\/b>([^<]+)

...但它只返回Unicode。我做错了什么?

1 个答案:

答案 0 :(得分:0)

这适用于任何有类似问题的人。答案是走过Stream ...我做的是以下内容:

// Create a temporary file, delete if it already exists
string MyFile = Path.GetTempPath() + v_fexpd.Dateiname;
if (File.Exists(MyFile)) {
    File.Delete(MyFile);
}

using (TextWriter tw = new StreamWriter(MyFile.ToString(), true, System.Text.Encoding.GetEncoding(1252)))
    tw.WriteLine(v_Inhalt);

context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Dateiname);
context.Response.AddHeader("Content-Type", "text/csv; charset=windows-1252");

// Write the file - which at this point is correctly-encoded - 
// directly into the output.
context.Response.WriteFile(MyFile);

context.Response.End();

// Leave no traces
File.Delete(MyFile);