jQuery 使用 JavaScript 在服务器上保存文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15722765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:40:06  来源:igfitidea点击:

Saving a text file on server using JavaScript

javascriptjqueryajaxtext

提问by razz

Is it possible to save text to a new text file using JavaScript/jQuery without using PHP? The text I'm trying to save may contain HTML entities, JS, HTML, CSS and PHP scripts that I don't want to escape or use urlencode!

是否可以在不使用 PHP 的情况下使用 JavaScript/jQuery 将文本保存到新的文本文件?我试图保存的文本可能包含我不想转义或使用 urlencode 的 HTML 实体、JS、HTML、CSS 和 PHP 脚本!

If it's only can be achieved using PHP how can I pass the text to PHP without encoding it?

如果只能使用 PHP 实现,我如何将文本传递给 PHP 而不对其进行编码?

回答by razz

You must have a server-side script to handle your request, it can't be done using javascript.

您必须有一个服务器端脚本来处理您的请求,它不能使用 javascript 来完成。

To send raw data without URIencoding or escaping special characters to the php and save it as new txtfile you can send ajax request using postmethod and FormDatalike:

要在没有 URIencoding 或转义特殊字符的情况下将原始数据发送到 php 并将其另存为新txt文件,您可以使用以下post方法发送 ajax 请求FormData

JS:

JS:

var data = new FormData();
data.append("data" , "the_text_you_want_to_save");
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', '/path/to/php', true );
xhr.send(data);

PHP:

PHP:

if(!empty($_POST['data'])){
$data = $_POST['data'];
$fname = mktime() . ".txt";//generates random name

$file = fopen("upload/" .$fname, 'w');//creates new file
fwrite($file, $data);
fclose($file);
}

Edit:

编辑:

As Florian mentioned below, the XHR fallback is not required since FormDatais not supported in older browsers (formdata browser compatibiltiy), so you can declare XHR variable as:

正如下面提到的 Florian,XHR 回退不是必需的,因为FormData旧浏览器(formdata browser compatibiltiy)不支持,因此您可以将 XHR 变量声明为:

var xhr = new XMLHttpRequest();

Also please note that this works only for browsers that support FormDatasuch as IE +10.

另请注意,这仅适用于支持FormDataIE +10 等浏览器。

回答by Joe F

It's not possible to save content to the website using only client-side scripting such as JavaScript and jQuery, but by submitting the data in an AJAX POST request you could perform the other half very easily on the server-side.

仅使用 JavaScript 和 jQuery 等客户端脚本无法将内容保存到网站,但是通过在 AJAX POST 请求中提交数据,您可以在服务器端非常轻松地执行另一半操作。

However, I would not recommend having raw content such as scripts so easily writeable to your hosting as this could easily be exploited. If you want to learn more about AJAX POST requests, you can read the jQuery API page:

但是,我不建议将原始内容(例如脚本)轻松写入您的主机,因为这很容易被利用。如果您想了解有关 AJAX POST 请求的更多信息,可以阅读 jQuery API 页面:

http://api.jquery.com/jQuery.post/

http://api.jquery.com/jQuery.post/

And here are some things you ought to be aware of if you still want to save raw script files on your hosting. You have to be very careful with security if you are handling files like this!

如果您仍想在主机上保存原始脚本文件,那么您应该注意以下几点。如果您处理这样的文件,您必须非常小心安全!

File uploading (most of this applies if sending plain text too if javascript can choose the name of the file) http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=securityhttps://www.owasp.org/index.php/Unrestricted_File_Upload

文件上传(如果 javascript 可以选择文件名,则大部分也适用于发送纯文本) http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=security https://www.owasp .org/index.php/Unrestricted_File_Upload