我正在使用 ASP.NET MVC 4 开发一个 Web 应用程序,该应用程序从 SQL Server 2008 数据库中检索图像数据/非结构化数据(扫描文档),以通过 Web 浏览器下载。它不用于在 Web 浏览器上查看,但应通过客户端计算机上的默认程序下载和查看。我的主要问题是数据在下载后无法被 PC 识别。需要帮助来解决这个问题。
这是代码片段。
模型
public bool getData(string code)
{
SqlCommand cmd = new SqlCommand("spReadDocumentCode", con);
cmd.CommandType = CommandType.StoredProcedure;
bool state = false;
cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value =
Convert.ToString(code);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
if (con.State == ConnectionState.Closed)
con.Open();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
image = (byte[])dt.Rows[0]["Data"];
// I pass the data to a public MemoryStream variable.
ms = new MemoryStream((byte[])dt.Rows[0]["Data"]);
state = true;
}
if (con.State == ConnectionState.Open)
con.Close();
return state;
}
catch
{
if (con.State == ConnectionState.Open)
con.Close();
return false;
}
控制器
[OutputCache(Duration=3600, VaryByParam="code", NoStore = true)]
public ActionResult Document(string code)
{
MemoryStream img = null;
if (Doc.getData(code))
{
img = Doc.getMemoryStream();
}
try
{
if (img == null)
{
return HttpNotFound();
}
// I send back an image file, however I want to upgrade the code
// to send back any type of scanned documents stored on the database.
return new FileStreamResult(img, "image/png");
}
catch
{
return null;
}
}