通过响应向客户端发送 Zip 文件 - 发送的类型不正确

发布于 2024-09-29 00:49:56 字数 1911 浏览 2 评论 0原文

好吧,多年来我一直在为这个问题烦恼。我有一个 WebService,它将 zip 文件发送到浏览器。这有效;当我测试 WebService 并直接通过它“调用”该方法时,zip 文件会正确下载到浏览器。

当我使用 jQuery 向 WebService 发送 AJAX 请求时,问题就出现了 - zip 文件被下载到响应中,但它保留在响应中并且不会作为文件下载。

这是我的代码:

        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ContentType = "application/zip";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;         filename=pleasework.zip");


        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory("c:\\inetpub\\mediaserver\\music\\mixes");
            zip.AddFile("c:\\test.txt");
            zip.AddFile("c:\\test2.txt");
            zip.Save("c:\\filename.zip");
        }
        HttpContext.Current.Response.TransmitFile("c:\\iloveshrooms.zip");
        return "done";        
}

现在这是有效的,因为当我直接导航到服务时使用“调用”功能时,文件会下载。

这是我的 jQuery AJAX 请求...

function getFile() {
            alert("sending POST request...");
            $.ajax({
                type:   'POST',
                url:    'ZipService.asmx/GetZipFile HTTP 1.1',
                contentType: "application/x-www-form-urlencoded",
                data:   '{ "path":"c:\\inetpub\\mediaserver\\music\\mixes"  }',
                dataType: '',
                beforeSend: function(xhr) {xhr.setRequestHeader("Accept","application/zip");},
                success: function() {alert(":-)");},
                failure: function() {alert(":-(");}
            });
        }

我在“beforeSend”中添加了代码,以便请求明确说明浏览器应该期望什么类型的响应。

我一直在使用 Firebug 来监视请求/响应标头,并且看不到它们有任何问题(除了当我查看响应的内容时,它充满了二进制数据并且其大小与我正在处理的文件相同) 我只是尝试上传

请求/响应标头和响应内容的屏幕转储,但我还没有足够的信誉点来执行此操作:-(

响应与 Zip 文件大小相同,因此我假设它被发送回浏览器,但浏览器不知道如何处理它,

并且在 IE、FF 和 Chrome 中进行了测试,结果是一致的,

如果有人能对此有所了解,我会非常高兴 。欣赏它!

OK, I've been pulling my hair out about this for ages. I have a WebService which sends a zip file to the browser. This works; when I test the WebService and 'invoke' the method through it directly, the zip file is downloaded to the browser correctly.

The problem arises when I use jQuery to send an AJAX request to the WebService - the zip file is downloaded to the response, but it stays in the response and doesn't download as a file.

Here is my code:

        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ContentType = "application/zip";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;         filename=pleasework.zip");


        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory("c:\\inetpub\\mediaserver\\music\\mixes");
            zip.AddFile("c:\\test.txt");
            zip.AddFile("c:\\test2.txt");
            zip.Save("c:\\filename.zip");
        }
        HttpContext.Current.Response.TransmitFile("c:\\iloveshrooms.zip");
        return "done";        
}

Now this works since the file downloads when I use the 'invoke' function when navigating directly to the service.

Here is my jQuery AJAX request...

function getFile() {
            alert("sending POST request...");
            $.ajax({
                type:   'POST',
                url:    'ZipService.asmx/GetZipFile HTTP 1.1',
                contentType: "application/x-www-form-urlencoded",
                data:   '{ "path":"c:\\inetpub\\mediaserver\\music\\mixes"  }',
                dataType: '',
                beforeSend: function(xhr) {xhr.setRequestHeader("Accept","application/zip");},
                success: function() {alert(":-)");},
                failure: function() {alert(":-(");}
            });
        }

I added the code in 'beforeSend' so that the request states explicitly what type of response the browser should expect.

I've been using Firebug to monitor the request/response headers and can't see anything wrong with them (except when I look at the content of the response, it's full of binary data AND its the same size as the file I'm sending.

I just attempted to upload screen-dumps of the request/response headers and the content of the reponse but I don't have enough reputation points to do this yet :-(

The Response is the same size of the Zip file so I'm assuming its being sent back to the browser, but the browser doesn't know what to do with it.

Tested in IE, FF & Chrome and the results are consistent.

If anyone could shed any light on this I'd greatly appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扭转时空 2024-10-06 00:49:56

有几个简单的解决方案。

将您的服务器过程转换为使用 GET 请求,您可以使用 window.open 来初始化下载。

window.open('your_procedure_url?parameter=xyz')

如果您想坚持使用 POST,您可以创建一个隐藏表单,其中包含每个参数的输入。将表单操作设置为 POST 到您的服务 URL,并使用 form.submit() 启动下载。

There are a couple of easy solutions.

Convert your server procedure to use a GET request, you can use window.open, to initialize the download.

window.open('your_procedure_url?parameter=xyz')

If you want stick with POST, you can create a hidden form, with inputs for each of the parameters. Set the form action to POST to your service url and use form.submit() to launch the download.

混浊又暗下来 2024-10-06 00:49:56

第一。不要将其作为网络服务。如果它总是提供 zip 文件,为什么它不是 aspx 或 ashx?脚本请求的资源不必是 ASMX。

第二。无法接收 AJAX 或 javascript 中的二进制文件是不正确的。你可以。例如,请参阅使用 Javascript 解压缩 zip 存档

但请确保您确实想要在脚本中处理 zip 文件。在大多数情况下,您希望让用户将 zip 文件保存到文件系统。

1st. Don't make it a webservice. If it always delivers a zip file, why is it not an aspx or a ashx? A resource that is requested by a script, need not be an ASMX.

2nd. it's not true that you cannot receive a binary file in AJAX or javascript. You can. For example, see Unzipping zip archives with Javascript.

But make sure you actually want to handle the zip file in script. In most cases you want to let the user save the zip file to the filesystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文