【发布时间】:2014-11-11 14:52:14
【问题描述】:
我正在使用具有文件附件表的 MS Dynamics 导航数据库。这些文件存储在 MS SQL 中。我可以使用我构建的自定义 asp.net 应用程序将文件拉到我的桌面,但是当我打开文件时,它们已损坏。这些是位于数据库“图像”文件类型列中的 PDF 文件,我已尝试下载 20 多个文件。它们的大小都不同,似乎下载成功。
我怀疑这些是 PDF 文件的原因是因为二进制列旁边的列给了我 PDF 格式的文件名。在我下载到不同的图像格式后,我也尝试重命名文件,但是当我尝试打开它时没有任何运气。这不是我第一个从 MS SQL 数据库检索二进制文件的项目。如果有人以前从 Nav 数据库中获取文件,请帮助我。当我在浏览器中给它一个特定的 ID 时,我编写了下面的示例代码来使用 LINQ to SQL 检索文件。如果您知道二进制文件本身中的任何类型的压缩或加密以及如何成功获取文件以读取它,请告诉我。谢谢
protected void getFileFromID(string queryid)
{
string Filename = string.Empty;
byte[] bytes;
try
{
DataClassesFilesDataContext dcontext = new DataClassesFilesDataContext();
var myfile = (from file in dcontext.Comptroller_File_Attachments
where file.No_ == queryid
select file).First();
if (myfile.Table_ID.ToString().Length > 0 && myfile.Attachment != null)
{
Filename = myfile.FileName.ToString();
bytes = myfile.Attachment.ToArray();
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename);
Response.BinaryWrite(bytes);
Response.End();
}
else
{
Response.Write("no file exist");
}
}
catch (Exception e)
{
Response.Write(e);
}
}
【问题讨论】:
标签: asp.net sql-server linq-to-sql navision