Pergunta

I am working on an asp.net application using Ext.Net for my UI and Ajax calls (DirectMethods). I am trying to download a zip file on the server using DotNetZip. There are two parts to the download process:

  • Part 1: First invoke method ExportXML to export in-memory XMLs to physical XML files on the server in a folder.

  • Part 2: On success of ExportXML method invoke DownloadZipFile. This method uses DotNetZip to zip these XML files and call Save method on the ZipFile object to invoke download on the browser.

Client side Ext.Net code

Ext.net.DirectMethods.ExportXML(ids, {
                success: function (zipID) {
                    Ext.net.DirectMethods.DownloadZipFile(zipID);
                },
                eventMask: {
                    showMask: true,
                    msg: "Creating XML files... This may take some time..."
                }
            });

Server side DirectMethods

[DirectMethod]
    public void DownloadZipFile(string randomFolderName)
    {
        var zippedXMLFiles = new ZipFile();
        var filePath = System.IO.Path.Combine(m_baseExportFolder, randomFolderName);

        Response.AddHeader("Content-Disposition", "attachment; filename=massExportXML.zip");
        Response.ContentType = "application/zip";
        var xmlDir = new DirectoryInfo(filePath);
        foreach (var fileInfo in xmlDir.GetFiles())
        {
            zippedXMLFiles.AddFile(fileInfo.FullName, string.Empty);
        }

        zippedXMLFiles.Save(Response.OutputStream);
    }

    [DirectMethod]


    public string ExportXML(string[] kCodes)
    {
        var folderID = System.IO.Path.GetRandomFileName();
        var folderPath = System.IO.Path.Combine(m_baseExportFolder, folderID);
        System.IO.Directory.CreateDirectory(folderPath);

        foreach (var kCode in kCodes)
        {
            GenerateXML(Convert.ToInt32(kCode), folderPath);
        }

        return folderID;
    }

GenerateXML is the method which converts the in-memory XML to the physical XML file.

My code runs successfully to create the zip file and add the zip files, however, on the browser I see

Error displayed when downloading the zip file

and HTTP Response Code as 200 (this means no problems with the request). I am so confused why this does not work.

Please help!

Thanks, Vaibinewbee

Foi útil?

Solução

The browser is probably expecting either a JSON or an XML response, but is instead getting a blob of binary data. You cannot, to my knowledge, download a file with an AJAX request, instead you need to cause the browser to navigate to a URL that will download a file.

This can be accomplished with something like a generic handler (If in asp.net webforms), or an action method that returns a file (if MVC). You can navigate from Javascript with window.location, or create a <a> tag with the href set to wherever downloads the file, pass whatever parameters you need as a query string, and then the browser will work as expected.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top