我正在尝试从SQL Server中检索varbinary
值数组,而不是尝试将它们存储在用户可以下载的文本文件中,但不确定如何将文件返回到Response.Redirect()
或如何返回文本文件。
public byte[] GenerateTextFile()
{
if (m_Template == CFCTemplate.GETTXTDATA)
{
SqlCommand cmd = new SqlCommand("SELECT SpecData FROM Spec WHERE TestID=" + SpecTestID);
InternalSalesDB.Instance.query(cmd);
DataTable dt = InternalSalesDB.Instance.query(cmd).Tables[0];
if (dt.Rows.Count > 0)
{
byte[] binarySpec = (byte[])dt.Rows[0][0];
FileStream fileStream = new FileStream("TestData.txt",FileMode.Append);
fileStream.Write(binarySpec, 0, binarySpec.Length);
fileStream.Flush();
fileStream.Close();
}
}
//need return statement here ??
}
答案 0 :(得分:1)
希望这有助于获得一个想法。在这里,它从数据表创建一个CSV文件,并使用Respose下载它。
private void WriteToCSV(DataTable dt, string fileName, string delimiter)
{
// Prepare the Output Stream
Response.Clear();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename={0}", fileName));
string value = "";
StringBuilder builder = new StringBuilder();
// Write the CSV Column Headers
for (int i = 0; i < dt.Columns.Count; i++)
{
value = dt.Columns[i].ColumnName;
// Implement special handling for values that contain comma or quote
// Enclose in quotes and double up any double quotes
if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
else
{
builder.Append(value);
}
Response.Write(value);
Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine);
builder.Clear();
}
//write the data
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
value = row[i].ToString();
// Implement special handling for values that contain comma or quote
// Enclose in quotes and double up any double quotes
if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
else
{
builder.Append(value);
}
Response.Write(builder.ToString());
Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine);
builder.Clear();
}
}
Response.End();
}
答案 1 :(得分:0)
为什么不让您的用户直接将varbinary下载为文本文件?
使用通用处理程序(.ashx)很容易实现当您从Project&gt;添加通用处理程序时,大多数代码都是由模板提供的。添加&gt;新商品......&gt;通用处理程序
Public Class DocHandler
Implements IHttpHandler, IRequiresSessionState
声明一个字节数组
Dim buffer() As Byte
如果需要,context参数允许您访问会话数据。在这个例子中,我访问文件名,但这可以很容易地从DB调用或作为处理程序的url参数。
Sub ProcessRequest(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
If context.Response.IsClientConnected Then
If context.Request.UrlReferrer IsNot Nothing Then
If context.Session("case_document_key") IsNot Nothing Then
在这里初始化你的数据库调用,我假设一个名为Cmd的SqlCommand变量
Dim yourvarbinaryfield = Cmd.ExecuteScalar()
将返回的varbinary字段转换为字节数组 buffer = DirectCast(yourvarbinaryfield,Byte())
这就是魔术 设置处理程序响应标头内容处置以将文件下载为文本:
context.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", context.Session("YourFileName")))
context.Response.ContentType = "text/plain"
context.Response.BinaryWrite(buffer)
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.Flush()
End If
End If
End If
End If
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
结束班