To store the word doc in SQL I have this:
byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length];
var storedFile = new email_attachment();
string strFullPath = uploader.UploadedFiles[0].FileName;
string strFileName = Path.GetFileName(strFullPath);
storedFile.email_attachment_id = Guid.NewGuid();
storedFile.emailer_id = new Guid(dropMailers.SelectedValue);
storedFile.file_name = strFileName;
storedFile.file_data = bytes;
db.email_attachments.InsertOnSubmit(storedFile);
db.SubmitChanges();
To get it back I use this:
var storedFile = db.email_attachments.Where(a => a.email_attachment_id.ToString() == dropAttachments.SelectedValue).Single();
string strPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Storage/Email/Attachments");
File.WriteAllBytes(Path.Combine(strPath, storedFile.file_name), storedFile.file_data.ToArray());
System.IO.FileInfo file = new System.IO.FileInfo(Path.Combine(strPath, storedFile.file_name));
if (file != null && file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
//Response.Write("This file does not exist.");
}
Problems: The doc I get back is blank, and I also believe saving it in the file system and then writing it to the response stream isn't very efficient. I'm thinking that step could be skipped. Any help or info would be much appreciated.