Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top