[
  {
    "Id": "491963",
    "ThreadId": "226809",
    "Html": "<p>I tried putting code that was running fine in a user control into a web service and now nothing happens..I'm never presente with download dialog box..How to I get this to</p>\r\n<p>work in a callback( web service..) here is my code..</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:black;background-color:white\">\r\n<pre><span style=\"color:blue\">Imports</span> System.Web\r\n<span style=\"color:blue\">Imports</span> System.Web.Services\r\n<span style=\"color:blue\">Imports</span> System.Web.Services.Protocols\r\n<span style=\"color:blue\">Imports</span> System.IO\r\n<span style=\"color:blue\">Imports</span> Ionic.Zip\r\n<span style=\"color:blue\">Imports</span> Microsoft.Win32\r\n<span style=\"color:blue\">Imports</span> System.Security.Permissions\r\n<span style=\"color:blue\">Imports</span> System.Xml\r\n\r\n\r\n\r\n<span style=\"color:green\">' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.</span>\r\n&lt;System.Web.Script.Services.ScriptService()&gt; _\r\n&lt;WebService(<span style=\"color:blue\">Namespace</span>:=<span style=\"color:#a31515\">&quot;http://tempuri.org/&quot;</span>)&gt; _\r\n&lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; _\r\n&lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; _\r\n<span style=\"color:blue\">Public</span> <span style=\"color:blue\">Class</span> DownLoadUIService\r\n    <span style=\"color:blue\">Inherits</span> System.Web.Services.WebService\r\n\r\n    <span style=\"color:blue\">Public</span> currFileName <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span> = <span style=\"color:#a31515\">&quot;&quot;</span> <span style=\"color:green\">' used if only one file needs to be added. Just send File. No need to to zip up file</span>\r\n    <span style=\"color:blue\">Public</span> zip <span style=\"color:blue\">As</span> ZipFile = <span style=\"color:blue\">New</span> ZipFile\r\n\r\n    <span style=\"color:green\">''' &lt;summary&gt;</span>\r\n    <span style=\"color:green\">''' </span>\r\n    <span style=\"color:green\">''' &lt;/summary&gt;</span>\r\n    <span style=\"color:green\">''' &lt;param name=&quot;xml&quot;&gt;&lt;/param&gt;</span>\r\n    <span style=\"color:green\">''' &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span>\r\n    <span style=\"color:green\">''' &lt;returns&gt;&lt;/returns&gt;</span>\r\n    <span style=\"color:green\">''' &lt;remarks&gt;&lt;/remarks&gt;</span>\r\n    &lt;WebMethod()&gt; _\r\n    <span style=\"color:blue\">Public</span> <span style=\"color:blue\">Sub</span> GetFiles(<span style=\"color:blue\">ByVal</span> xml <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span>, <span style=\"color:blue\">ByVal</span> value() <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span>)\r\n        <span style=\"color:blue\">Dim</span> xmlFilePath = Server.MapPath(<span style=\"color:#a31515\">&quot;~&quot;</span>) + xml\r\n\r\n        <span style=\"color:blue\">Dim</span> doc <span style=\"color:blue\">As</span> System.Xml.XmlDocument\r\n        <span style=\"color:blue\">Dim</span> m_node <span style=\"color:blue\">As</span> System.Xml.XmlNode\r\n\r\n        <span style=\"color:green\">'Create the XML Document</span>\r\n        doc = <span style=\"color:blue\">New</span> System.Xml.XmlDocument()\r\n        <span style=\"color:green\">'Load the Xml file</span>\r\n        doc.Load(xmlFilePath)\r\n        <span style=\"color:blue\">Dim</span> root <span style=\"color:blue\">As</span> System.Xml.XmlNode = doc.DocumentElement\r\n\r\n        <span style=\"color:blue\">For</span> <span style=\"color:blue\">Each</span> strVal <span style=\"color:blue\">In</span> value\r\n            <span style=\"color:blue\">Dim</span> strProductLookupValue <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span> = strVal.ToString.Substring(0, strVal.ToString.LastIndexOf(<span style=\"color:#a31515\">&quot;-&quot;</span>))\r\n            <span style=\"color:blue\">Dim</span> strFileLookupValue <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span> = strVal.ToString.Substring(strVal.ToString.LastIndexOf(<span style=\"color:#a31515\">&quot;-&quot;</span>) + 1)\r\n            <span style=\"color:blue\">Dim</span> strXPATH = <span style=\"color:#a31515\">&quot;PRODUCT[@id='&quot;</span> + strProductLookupValue + <span style=\"color:#a31515\">&quot;']/FILES/PATH[@id='&quot;</span> + strFileLookupValue + <span style=\"color:#a31515\">&quot;']&quot;</span>\r\n\r\n            m_node = root.SelectSingleNode(strXPATH)\r\n\r\n            <span style=\"color:blue\">Dim</span> rootDir <span style=\"color:blue\">As</span> <span style=\"color:blue\">New</span> DirectoryInfo(m_node.LastChild.InnerText.ToString)\r\n\r\n            MsgBox(m_node.LastChild.InnerText.ToString)\r\n            <span style=\"color:green\">'retrieve files in DIRs recursively</span>\r\n            WorkWithDirectory(rootDir)\r\n\r\n\r\n        <span style=\"color:blue\">Next</span>\r\n\r\n        <span style=\"color:blue\">Dim</span> archiveName <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span> = <span style=\"color:#a31515\">&quot;&quot;</span>\r\n        HttpContext.Current.Response.Clear()\r\n        HttpContext.Current.Response.BufferOutput = <span style=\"color:blue\">False</span>\r\n\r\n        <span style=\"color:blue\">Dim</span> strArr <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span>() = currFileName.Split(<span style=\"color:#a31515\">&quot;\\&quot;</span>)\r\n\r\n        <span style=\"color:blue\">If</span> zip.<span style=\"color:blue\">Count</span> = 1 <span style=\"color:blue\">Then</span>\r\n            HttpContext.Current.Response.ContentType = GetMIMEType(currFileName)\r\n\r\n            archiveName = strArr(strArr.Length - 1)\r\n        <span style=\"color:blue\">Else</span>\r\n            HttpContext.Current.Response.ContentType = <span style=\"color:#a31515\">&quot;application/zip&quot;</span>\r\n            archiveName = <span style=\"color:blue\">String</span>.Format(<span style=\"color:#a31515\">&quot;archive-{0}.zip&quot;</span>, DateTime.Now.ToString(<span style=\"color:#a31515\">&quot;yyyy-MMM-dd-HHmmss&quot;</span>))\r\n        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n\r\n        HttpContext.Current.Response.AddHeader(<span style=\"color:#a31515\">&quot;content-disposition&quot;</span>, <span style=\"color:#a31515\">&quot;attachment; filename=&quot;</span> + archiveName)\r\n        <span style=\"color:blue\">If</span> zip.<span style=\"color:blue\">Count</span> = 1 <span style=\"color:blue\">Then</span>\r\n            HttpContext.Current.Response.WriteFile(currFileName)\r\n\r\n        <span style=\"color:blue\">Else</span>\r\n            zip.Save(HttpContext.Current.Response.OutputStream)\r\n        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n\r\n        HttpContext.Current.Response.Close()\r\n        <span style=\"color:green\">'Return True</span>\r\n\r\n    <span style=\"color:blue\">End</span> <span style=\"color:blue\">Sub</span>\r\n</pre>\r\n</div>",
    "PostedDate": "2010-09-10T13:20:40.24-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "492592",
    "ThreadId": "226809",
    "Html": "<p>anyone? Can you help cheeso...Thanks..</p>",
    "PostedDate": "2010-09-12T18:42:19.51-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "492794",
    "ThreadId": "226809",
    "Html": "<p>I came across another post but having a hard time understanding it..using a stream is the user ever presented with a dialog box to download the file?</p>\r\n<p>&nbsp;</p>\r\n<pre><span style=\"color:blue\">public</span> Stream GetFileZip(<span style=\"color:blue\">string</span> file, <span style=\"color:blue\">int</span> copies)\r\n{\r\n    <span style=\"color:blue\">return</span> GetPipedStream(s =&gt;\r\n        {\r\n            <span style=\"color:blue\">int</span> n;\r\n            <span style=\"color:blue\">byte</span>[] bytes = <span style=\"color:blue\">new</span> <span style=\"color:blue\">byte</span>[1024]; <span style=\"color:green\">// any size will do</span>\r\n            <span style=\"color:blue\">using</span> (ZipOutputStream zos = <span style=\"color:blue\">new</span> ZipOutputStream(s, <span style=\"color:blue\">true</span>))\r\n            {\r\n                <span style=\"color:blue\">for</span> (<span style=\"color:blue\">int</span> i = 1; i &lt;= copies; i++)\r\n                {\r\n                    zos.PutNextEntry(<span style=\"color:#a31515\">&quot;Copy&quot;</span> + i);\r\n                    <span style=\"color:blue\">using</span> (FileStream fs = File.OpenRead(file))\r\n                    {\r\n                        <span style=\"color:blue\">while</span>((n = fs.Read(bytes,0,bytes.Length))&gt;0)\r\n                        {\r\n                            zos.Write(bytes, 0, n);\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        });\r\n}</pre>",
    "PostedDate": "2010-09-13T07:24:36.883-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "495659",
    "ThreadId": "226809",
    "Html": "<p>&nbsp;</p>\r\n<p>First, let's address your original code. &nbsp;The code you have that deals with zip files, is obviously correct. If it works nicely in a usercontrol, then you know it is correct, right? &nbsp;But a webservice is a different animal thamn a user control. &nbsp;Take a moment and consider the differences.&nbsp;</p>\r\n<p>A user control is a browser/UI element. &nbsp;It renders UI to the browser when requested. In your case, I suppose your user control doesn't render HTML UI, &nbsp;but instead returns some HTTP headers that eventually cause the browser to popup the &quot;Save File As...?&quot; prompt. &nbsp;</p>\r\n<p>A web service is NOT a UI element. &nbsp;It is a service, and specifically it&nbsp;<em>may not</em>&nbsp;render UI. &nbsp; A webservice is a thing that receives a message, does some work, and then responds with another message. Typically these messages are encoded as XML. &nbsp;A webservice may be used by some browser-side javascript, which then renders UI. &nbsp;But a webservice cannot render UI, and must not attempt to do so. &nbsp;That includes the UI that results from an HTTP response that, when returned from a usercontrol, would result in a &quot;Save File As...?&quot; prompt.&nbsp;</p>\r\n<p>So, you have to consider what you really want to do. &nbsp;What's the purpose of putting the zipfile creation logic into a webservice? &nbsp;What are you really trying to do there?&nbsp;</p>\r\n<p>----</p>\r\n<p>Now, as for your 2nd question - I believe that is code that is designed for use within a WCF service. &nbsp;Which you are not using, as far as I can tell. &nbsp;(You're using ASPNET). &nbsp;That's not going to help you. &nbsp;</p>\r\n<p>First thing: You need to resolve the design issue I raised.</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-09-19T11:02:50.633-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "498287",
    "ThreadId": "226809",
    "Html": "<p>Thanks Cheeso..I see what you mean about my design issue..I ended up just using a user control which zips up the file and then uses a download handler to present file to the user</p>\r\n<p>as a link..IE. download.ashx?file=name of file..I trying to think of the best way to delete the file now once the user has downloaded it..Is using a session a good solution and once session is up delete the zip files ? I also don't know what to do to implement a denial of service attack of some sort where the user or program would simulate calling my create zip page to create a bunch of zip files of my server..any suggestions?</p>",
    "PostedDate": "2010-09-24T17:02:50.757-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "499737",
    "ThreadId": "226809",
    "Html": "You can safely delete the file after Response.Close.  I think the .ashx has a Response.Close, doesn't it? \r\n\r\nYou can't do it after Response.End, because that call throws an exception, which means logic that follows it will never be executed. \r\n\r\nThis question has been confronted multiple times on the DNZ forums; you may find some good ideas or insight be seat hong the archives.\r\n\r\nGood luck.\r\n\r\n",
    "PostedDate": "2010-09-28T21:15:45.72-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]