使用 JS、jQuery,如何将 AJAX 响应保存到(文本)文件?

发布于 2024-09-17 13:00:12 字数 335 浏览 6 评论 0原文

似乎这个问题会定期被问到,常见的回答是“无论如何,您不应该使用 AJAX 这样做。只需将窗口位置设置为文件即可。”

但我试图请求一个实际上并不存在于服务器上任何位置的文件。它是根据 GET/POST 上下文参数动态生成的(通过 Django 视图)。我想要通过 AJAX 检索然后保存到客户端计算机的文件是一个文本文件 (csv)。

我目前可以将文本发送到客户端计算机(并且可以通过在日志记录或警报中查看它来验证这一点),但无法弄清楚如何将此文本保存到 AJAX 成功回调 fn 内的文件中。

本质上,这可能吗,是 JS 可以做的事情吗?也就是说,打开实际上是 AJAX 响应文本的“文件”的文件保存对话框?

It seems like this question is asked periodically and the common response is "You shouldn't do that with AJAX anyway. Just set the window location to the file."

But I'm trying to request a file that doesn't actually exist out on the server anywhere. It's dynamically generated (by a Django view) given the GET/POST context parameters. The file I want to retrieve via AJAX, and then save to the client machine, is a text file (csv).

I can currently get the text to the client machine (and can verify this by seeing it in logging or an alert) but cannot then figure out how to save this text to a file inside of the AJAX success callback fn.

Essentially, is this possible, is it something JS can do? That is, to open file save dialogs for "files" that are actually AJAX response text?

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

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

发布评论

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

评论(6

爱本泡沫多脆弱 2024-09-24 13:02:03

我有同样的问题。你可以试试这个

<button id="Save">Save</button>
<img src="MakeThumbnail.ashx?Image=1.jpg" id="imgCrop">

$("#Save").click(function (e) {
    url = $("#imgCrop").attr("src")+"&Action=Save"
    e.preventDefault();  //stop the browser from following
    window.location.href = url;
});

I have the same problem. You can try this

<button id="Save">Save</button>
<img src="MakeThumbnail.ashx?Image=1.jpg" id="imgCrop">

$("#Save").click(function (e) {
    url = $("#imgCrop").attr("src")+"&Action=Save"
    e.preventDefault();  //stop the browser from following
    window.location.href = url;
});
记忆之渊 2024-09-24 13:01:49

您不能为此使用 PHP rename() 函数吗? JavaScript?调用 PHP 文件并传递要复制的文件的名称以及位置作为参数?

Could you not use the PHP rename() function for this, instead of just Javascript? Call to a PHP file and pass the name of the file you want to copy along with where as parameters?

空心↖ 2024-09-24 13:01:40

基本上,没有。由于安全限制,Javascript 无法将任何内容保存到本地计算机。最好的选择可能是拥有一个用户可以信任的签名小程序来写入文件,或者将其放在文本区域中,然后他们可以轻松地将其复制并粘贴到新文件中。

Basically, no. Javascript cant save anything to the local machine due to security restrictions. Your best bet may be to have a signed applet that the user can trust to write the file, or put it in a textarea that they can then easily copy and paste into a new file.

看海 2024-09-24 13:01:25

出于安全原因,这不是 JavaScript(以及 jQuery 或任何其他 JS 框架)被允许执行的操作。你也许可以做你想做的事情或其他路线,但不能是 JavaScript。请记住,Flash 对此也有其自己的一系列安全限制。

(是的,IE 可以通过 ActiveX 对象执行此操作,但我不将其视为此处的“解决方案”)

This isn't something JavaScript (and therefore jQuery or anything other JS framework) is allowed to do, for security reasons. You may be able to do what you want to flash or another route, but not JavaScript. Bear in mind Flash has it's own slew of security restrictions for this as well.

(Yes, IE can do this via an ActiveX object, but I'm not counting that as a "solution" here)

淑女气质 2024-09-24 13:01:15

正如其他人提到的,你不能仅使用 JavaScript 来做到这一点。

IMO 最好的选择是 Flash 10+ FileReference API

有一些很好的 JavaScript 包装器库,例如 Downloadify,它们提供了 JavaScript API 来访问这些方法。

看看这个演示

As others mentioned, you can't do it only with JavaScript.

IMO the best option would be the Flash 10+ FileReference API.

There are some good JavaScript wrapper libraries like Downloadify that provide a JavaScript API to access those methods.

Give a look to this demo.

独木成林 2024-09-24 13:01:05

从浏览器的角度来看,文件是否存在并不重要,它只是它请求的服务器上的资源。我认为您将需要执行某种版本的“只需将窗口位置设置为文件”。如果您将标头中的内容类型设置为浏览器无法识别的内容,我相信它会询问用户是否要保存它。

From the browser's point of view, it doesn't matter if the file exists or not, it's just a resource on a server that it's requesting. I think you're going to need to do some version of "Just set the window location to the file". If you set the content type in the header to something that the browser doesn't recognize, I believe it will ask the user if they want to save it.

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