0

长期潜伏者第一次海报。与.Net / Linq 合作了几年,所以我确定我在这里遗漏了一些东西。经过无数小时的研究,我需要帮助。我的代码基于 https 的建议:http: //damieng.com/blog/2010/01/11/linq-to-sql-tips-and-tricks-3

以下代码当前将存储在 sql 数据库中的选定文件(pdf、doc、png 等)保存到 C:\temp。效果很好。我想更进一步。我可以让浏览器提示而不是自动将其保存到 c:\temp,以便他们可以将其保存到所需的位置。

  {     
   var getFile = new myDataClass();

   //retrieve attachment id from selected row
   int attachmentId = Convert.ToInt32((this.gvAttachments.SelectedRow.Cells[1].Text));

    //retrieve attachment information from dataclass (sql attachment table)
    var results = from file in getFile.AttachmentsContents
                       where file.Attachment_Id == attachmentId
                       select file;

    string writePath = @"c:\temp";

   var myFile = results.First(); 

   File.WriteAllBytes(Path.Combine(writePath, myFile.attach_Name), myFile.attach_Data.ToArray());
}

因此,我可以代替使用 File.WriteAllBytes 来获取从我的 linq 查询(myFile)返回的数据并将其传递给会提示用户保存文件的东西吗?)。这个返回的对象可以与 response.transmitfile 一起使用吗?非常感谢。

4

1 回答 1

0

只需使用该BinaryWrite(myFile.attach_Data.ToArray())方法发送数据,因为它已经在内存中。

但首先要适当地设置标题,例如:

"Content-Disposition", "attachment; filename="+myFile.attach_Name
"Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

内容类型指导接收系统如何处理文件。这里有更多的MS Office 内容类型。如果在存储数据时它们是已知的,那么也应该存储内容类型。

此外,由于文件内容是您在响应中想要的唯一数据,请调用Clearbefore 和Endafter BinaryWrite

于 2013-04-25T16:37:40.400 回答