简体   繁体   English

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

[英]With JS, jQuery, how do I save an AJAX response to a (text) file?

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." 似乎这个问题是定期询问的,而且常见的回答是“你不应该用AJAX这样做。只需将窗口位置设置为文件即可。”

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. 它是在给定GET / POST上下文参数的情况下动态生成的(通过Django视图)。 The file I want to retrieve via AJAX, and then save to the client machine, is a text file (csv). 我想通过AJAX检索的文件,然后保存到客户端机器,是一个文本文件(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. 我现在可以将文本发送到客户端计算机(并且可以通过在日志记录或警报中查看它来验证这一点)但是无法弄清楚如何将此文本保存到AJAX成功回调fn内的文件中。

Essentially, is this possible, is it something JS can do? 从本质上讲,这是可能的,JS可以做什么? That is, to open file save dialogs for "files" that are actually AJAX response text? 也就是说,打开实际是AJAX响应文本的“文件”的文件保存对话框?

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. 如果您将标题中的内容类型设置为浏览器无法识别的内容,我相信它会询问用户是否要保存它。

As others mentioned, you can't do it only with JavaScript. 正如其他人提到的,你不能只用JavaScript做到这一点。

IMO the best option would be the Flash 10+ FileReference API . IMO最好的选择是Flash 10+ FileReference API

There are some good JavaScript wrapper libraries like Downloadify that provide a JavaScript API to access those methods. 有一些很好的JavaScript包装器库,如Downloadify ,它们提供了一个JavaScript API来访问这些方法。

Give a look to this demo . 看看这个演示

This isn't something JavaScript (and therefore jQuery or anything other JS framework) is allowed to do, for security reasons. 出于安全原因,这不是JavaScript(因此jQuery或任何其他JS框架)可以做的事情。 You may be able to do what you want to flash or another route, but not JavaScript. 您可能能够执行您想要闪存或其他路线的操作,但不能使用JavaScript。 Bear in mind Flash has it's own slew of security restrictions for this as well. 请记住,Flash也有自己的一系列安全限制。

(Yes, IE can do this via an ActiveX object, but I'm not counting that as a "solution" here) (是的,IE可以通过ActiveX对象执行此操作,但我不会将其视为“解决方案”)

Basically, no. 基本上没有。 Javascript cant save anything to the local machine due to security restrictions. 由于安全限制,Javascript无法将任何内容保存到本地计算机。 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. 您最好的选择可能是拥有一个用户可以信任的签名小程序来编写文件,或者将其放在textarea中,然后可以轻松地将其复制并粘贴到新文件中。

Could you not use the PHP rename() function for this, instead of just Javascript? 你可以不使用PHP rename()函数,而不仅仅是Javascript吗? Call to a PHP file and pass the name of the file you want to copy along with where as parameters? 调用PHP文件并传递要复制的文件的名称以及作为参数的位置?

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM