<p>I am looking to stream a file housed in a SharePoint 2003 document library down to the browser.  Basically the idea is to open the file as a stream and then to "write" the file stream to the reponse, specifying the content type and content disposition headers.  Content disposition is used to preserve the file name, content type of course to clue the browser about what app to open to view the file.  </p>

<p>This works all good and fine in a development environment and UAT environment.  However, in the production environment, things do not always work as expected,however only with IE6/IE7. FF works great in all environments. </p>

<p>Note that in the production environment SSL is enabled and generally used.  (When SSL is not used in the production environment, file streams, is named as expected, and properly dislays.)</p>

<p>Here is a code snippet:</p>

<pre><code>System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(".") + "\\" + "test.doc", System.IO.FileMode.Open);
long byteNum = fs.Length;
byte[] pdfBytes = new byte[byteNum];
fs.Read(pdfBytes, 0, (int)byteNum);

Response.AppendHeader("Content-disposition", "filename=Testme.doc");
Response.CacheControl = "no-cache";
Response.ContentType = "application/msword; charset=utf-8";
Response.Expires = -1;
Response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);
Response.Flush();
Response.Close();
fs.Close();
</code></pre>

<p>Like I said, this code snippet works fine on the dev machine and in the UAT environment.  A dialog box opens and asks to save, view or cancel Testme.doc.  But in production onnly when using SSL, IE 6 &amp; IE7 don't use the name of the attachment.  Instead it uses the name of the page that is sending the stream, testheader.apx and then an error is thrown.  </p>

<p>IE does provide an advanced setting "Do not save encrypted pages to disk".  </p>

<p>I suspect this is part of the problem, the server tells the browser not to cache the file, while IE has the "Do not save encrypted pages to disk" enabled.</p>

<p>Yes I am aware that for larger files, the code snippet above will be a major drag on memory and this implimentation will be problematic. So the real final solution will not open the entire file into a single byte array, but rather will open the file as a stream, and then send the file down to the client in bite size chunks (e.g. perhaps roughly 10K in size).</p>

<p>Anyone else have similar experience "streaming" binary files over ssl?  Any suggestions or recommendations?</p>

[{'answer_id': 53078, 'author': 'Mauro', 'author_id': 2208, 'author_profile': 'https://Stackoverflow.com/users/2208', 'pm_score': 2, 'selected': False, 'text': '<p>It might be something really simple, believe it or not I coded exactly the same thing today, i think the issue might be that the content disposition doesnt tell the browser its an attachment and therefore able to be saved.</p>\n\n<pre><code>\nResponse.AddHeader("Content-Disposition", "attachment;filename=myfile.doc");\n</code></pre>\n\n<p>failing that i\'ve included my code below as I know that works over https://</p>\n\n<pre>\n<code>\nprivate void ReadFile(string URL)\n{\n  try\n  {\n                string uristring = URL;\n                WebRequest myReq = WebRequest.Create(uristring);\n                NetworkCredential netCredential = new NetworkCredential(ConfigurationManager.AppSettings["Username"].ToString(), \n                                                                        ConfigurationManager.AppSettings["Password"].ToString(), \n                                                                        ConfigurationManager.AppSettings["Domain"].ToString());\n                myReq.Credentials = netCredential;\n                StringBuilder strSource = new StringBuilder("");\n\n                //get the stream of data \n                string contentType = "";\n                MemoryStream ms;\n                // Send a request to download the pdf document and then get the response\n                using (HttpWebResponse response = (HttpWebResponse)myReq.GetResponse())\n                {\n                    contentType = response.ContentType;\n                    // Get the stream from the server\n                    using (Stream stream = response.GetResponseStream())\n                    {\n                        // Use the ReadFully method from the link above:\n                        byte[] data = ReadFully(stream, response.ContentLength);\n                        // Return the memory stream.\n                        ms = new MemoryStream(data);\n                    }\n                }\n\n                Response.Clear();\n                Response.ContentType = contentType;\n                Response.AddHeader("Content-Disposition", "attachment;");\n\n                // Write the memory stream containing the pdf file directly to the Response object that gets sent to the client\n                ms.WriteTo(Response.OutputStream);\n  }\n  catch (Exception ex)\n  {\n    throw new Exception("Error in ReadFile", ex);\n  }\n}\n</code>\n</pre>\n'}, {'answer_id': 77958, 'author': 'Jon', 'author_id': 4764, 'author_profile': 'https://Stackoverflow.com/users/4764', 'pm_score': 2, 'selected': False, 'text': '<p>Ok, I resolved the problem, several factors at play here.</p>\n\n<p>Firstly this support Microsoft article was beneficial:\n<a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;316431" rel="nofollow noreferrer">Internet Explorer is unable to open Office documents from an SSL Web site</a>.</p>\n\n<blockquote>\n  <p><em>In order for Internet Explorer to open documents in Office (or any out-of-process, ActiveX document server), Internet Explorer must save the file to the local cache directory and ask the associated application to load the file by using IPersistFile::Load. If the file is not stored to disk, this operation fails.</em></p>\n  \n  <p><em>When Internet Explorer communicates with a secure Web site through SSL, Internet Explorer enforces any no-cache request. If the header or headers are present, Internet Explorer does not cache the file. Consequently, Office cannot open the file.</em> </p>\n</blockquote>\n\n<p>Secondly, something earlier in the page processing was causing the "no-cache" header to get written.  So Response.ClearHeaders needed to be added, this cleared out the no-cache header, and the output of the page needs to allow caching.</p>\n\n<p>Thirdly for good measure, also added on Response.End, so that no other processing futher on in the request lifetime attempts to clear the headers I\'ve set and re-add the no-cache header.  </p>\n\n<p>Fourthly, discovered that content expiration had been enabled in IIS.  I\'ve left it enabled at the web site level, but since this one aspx page will serve as a gateway for downloading the files, I disabled it at the download page level.</p>\n\n<p>So here is the code snippet that works (there are a couple other minor changes which I believe are inconsequential):</p>\n\n<pre><code>System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(".") + "\\\\" + "TestMe.doc", System.IO.FileMode.Open);\nlong byteNum = fs.Length;\nbyte[] fileBytes = new byte[byteNum];\nfs.Read(fileBytes, 0, (int)byteNum);\n\nResponse.ClearContent();\nResponse.ClearHeaders();\nResponse.AppendHeader("Content-disposition", "attachment; filename=Testme.doc");\nResponse.Cache.SetCacheability(HttpCacheability.Public);\nResponse.ContentType = "application/octet-stream";\nResponse.OutputStream.Write(fileBytes, 0, fileBytes.Length);\nResponse.Flush();\nResponse.Close();\nfs.Close();\nResponse.End();\n</code></pre>\n\n<p>Keep in mind too, this is just for illustration.  The real production code will include exception handling and likely read the file a chunk at a time (perhaps 10K).</p>\n\n<p>Mauro, thanks for catching a detail that was missing from the code as well.</p>\n'}]
['https://Stackoverflow.com/questions/52702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4764/']