in web service

Sep 10, 2010 at 8:20 PM

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

work in a callback( web service..) here is my code..


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
Imports Ionic.Zip
Imports Microsoft.Win32
Imports System.Security.Permissions
Imports System.Xml

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class DownLoadUIService
    Inherits System.Web.Services.WebService

    Public currFileName As String = "" ' used if only one file needs to be added. Just send File. No need to to zip up file
    Public zip As ZipFile = New ZipFile

    ''' <summary>
    ''' </summary>
    ''' <param name="xml"></param>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <WebMethod()> _
    Public Sub GetFiles(ByVal xml As String, ByVal value() As String)
        Dim xmlFilePath = Server.MapPath("~") + xml

        Dim doc As System.Xml.XmlDocument
        Dim m_node As System.Xml.XmlNode

        'Create the XML Document
        doc = New System.Xml.XmlDocument()
        'Load the Xml file
        Dim root As System.Xml.XmlNode = doc.DocumentElement

        For Each strVal In value
            Dim strProductLookupValue As String = strVal.ToString.Substring(0, strVal.ToString.LastIndexOf("-"))
            Dim strFileLookupValue As String = strVal.ToString.Substring(strVal.ToString.LastIndexOf("-") + 1)
            Dim strXPATH = "PRODUCT[@id='" + strProductLookupValue + "']/FILES/PATH[@id='" + strFileLookupValue + "']"

            m_node = root.SelectSingleNode(strXPATH)

            Dim rootDir As New DirectoryInfo(m_node.LastChild.InnerText.ToString)

            'retrieve files in DIRs recursively


        Dim archiveName As String = ""
        HttpContext.Current.Response.BufferOutput = False

        Dim strArr As String() = currFileName.Split("\")

        If zip.Count = 1 Then
            HttpContext.Current.Response.ContentType = GetMIMEType(currFileName)

            archiveName = strArr(strArr.Length - 1)
            HttpContext.Current.Response.ContentType = "application/zip"
            archiveName = String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
        End If

        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + archiveName)
        If zip.Count = 1 Then

        End If

        'Return True

    End Sub
Sep 13, 2010 at 1:42 AM

anyone? Can you help cheeso...Thanks..

Sep 13, 2010 at 2:24 PM

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?


public Stream GetFileZip(string file, int copies)
    return GetPipedStream(s =>
            int n;
            byte[] bytes = new byte[1024]; // any size will do
            using (ZipOutputStream zos = new ZipOutputStream(s, true))
                for (int i = 1; i <= copies; i++)
                    zos.PutNextEntry("Copy" + i);
                    using (FileStream fs = File.OpenRead(file))
                        while((n = fs.Read(bytes,0,bytes.Length))>0)
                            zos.Write(bytes, 0, n);
Sep 19, 2010 at 6:02 PM


First, let's address your original code.  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?  But a webservice is a different animal thamn a user control.  Take a moment and consider the differences. 

A user control is a browser/UI element.  It renders UI to the browser when requested. In your case, I suppose your user control doesn't render HTML UI,  but instead returns some HTTP headers that eventually cause the browser to popup the "Save File As...?" prompt.  

A web service is NOT a UI element.  It is a service, and specifically it may not render UI.   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.  A webservice may be used by some browser-side javascript, which then renders UI.  But a webservice cannot render UI, and must not attempt to do so.  That includes the UI that results from an HTTP response that, when returned from a usercontrol, would result in a "Save File As...?" prompt. 

So, you have to consider what you really want to do.  What's the purpose of putting the zipfile creation logic into a webservice?  What are you really trying to do there? 


Now, as for your 2nd question - I believe that is code that is designed for use within a WCF service.  Which you are not using, as far as I can tell.  (You're using ASPNET).  That's not going to help you.  

First thing: You need to resolve the design issue I raised.



Sep 25, 2010 at 12:02 AM

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

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?

Sep 29, 2010 at 4:15 AM
You can safely delete the file after Response.Close. I think the .ashx has a Response.Close, doesn't it? You can't do it after Response.End, because that call throws an exception, which means logic that follows it will never be executed. This question has been confronted multiple times on the DNZ forums; you may find some good ideas or insight be seat hong the archives. Good luck.