2013-04-25 55 views
0

长时间潜伏者第一次海报。使用.Net/Linq几年,所以我确信我在这里错过了一些东西。经过无数小时的研究,我需要帮助。 我基于从https一个建议我的代码:其被存储在SQL数据库中的C http://damieng.com/blog/2010/01/11/linq-to-sql-tips-and-tricks-3保存文件提示而不是FileWriteAllBytes

下面的代码目前保存选定的文件(PDF,DOC,PNG等):\ 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一起使用吗?非常感谢。

回答

0

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

但第一组头适当,例如:

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

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

此外,由于文件内容是您希望在响应中唯一的数据,因此请拨Clear之前和End之后BinaryWrite

+0

完美的工作!非常感谢! – DysonGuy 2013-04-25 17:08:06

+0

我跳了枪!一旦我打开下载的文件,它已经损坏。我已经尝试过使用docx和doc。出于测试目的,我保留了File.WriteAllBytes代码,如果我从c:\ temp打开文档,它们就会打开。所以我仍然错过了 Response.Write(myFile.attach_Data.ToArray()); – DysonGuy 2013-04-25 20:27:17

+0

在我寻求解决方案时,我将Response.Write改为Response.BinaryWrite。 doc文件和pdf文件现在开放正常,但docx不太好。 – DysonGuy 2013-04-25 20:46:39