0

Long time lurker first time poster. Working with .Net / Linq for just a few years so I'm sure I'm missing something here. After countless hours of research I need help. I based my code on a suggestion from https:http://damieng.com/blog/2010/01/11/linq-to-sql-tips-and-tricks-3

The following code currently saves a chosen file (pdf, doc, png, etc) which is stored in an sql database to the C:\temp. Works great. I want to take it one step further. Instead of saving it automatically to the c:\temp can I have the browser prompt so they can save it to their desired location.

  {     
   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());
}

So instead of using File.WriteAllBytes can I instead take the data returned from my linq Query (myFile) and pass it into something that would prompt for the user to save the file instead?). Can this returned object be used with response.transmitfile? Thanks so much.

DysonGuy
  • 163
  • 1
  • 11

1 Answers1

0

Just use the BinaryWrite(myFile.attach_Data.ToArray()) method to send the data since it is already in memory.

But first set headers appropriately, for example:

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

Content-type guides the receiving system on how it should handle the file. Here are more MS Office content types. If they are known at the point the data is stored, the content-type should be stored, too.

Also, since the file content is the only data you want in the response, call Clear before and End after BinaryWrite.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • I jumped the gun! Once I open the downloaded file it's corrupt. I've tried with both a docx and doc. For testing purposes I kept the File.WriteAllBytes code in place and if I go an open the document from c:\temp they open fine. So I'm missing something still with Response.Write(myFile.attach_Data.ToArray()); – DysonGuy Apr 25 '13 at 20:27
  • In my quest for resolution I changed the Response.Write to Response.BinaryWrite. The doc file and pdf files open fine now yet the docx not so well. – DysonGuy Apr 25 '13 at 20:46
  • Adding the Reponse.Clear() and Response.AddHeader() That does appear to do the trick. The only difference is my content-type was left set as attachment and so far opening pdf, docx, and doc files works without a problem. If other files types like XLS cause problems I may have to look further into the content-type like you posted above. Thanks – DysonGuy Apr 26 '13 at 12:57