使用jquery导入到记事本 [英] Import to notepad using jquery

查看:119
本文介绍了使用jquery导入到记事本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有将数据导出到记事本的链接?
我有一些字段,如
名称,
年龄和
WorkingStatus



这些是text和textarea ...



我想将这些数据插入记事本中。是否有任何演示或代码可用?

解决方案

我不知道有什么方法让浏览器打开记事本,但您可以使用HTML5功能将文件保存为文本,然后在自己的记事本中打开它。根据浏览器的不同,您可能需要触发将文件保存在用户端。这里有两个参考文献,我会总结一下:

http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a HTML5-uses-javascrip /

/保存生成的文件在客户端rel =nofollow> http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side a>



基本上,您想要使用文字创建并保存一个blob。它应该看起来像这样:

  var arrayOfStuff = []; 
arrayOfStuff.push(姓名年龄工作状态);
arrayOfStuff.push(----------------------------------------- ------);
arrayOfStuff.push(document.getElementById(name)。value);
// etc
var blob = new Blob(arrayOfStuff,{type:'text / plain'});

//其余部分直接从wordpress链接复制)
var downloadLink = document.createElement(a);
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML =下载档案;
if(window.webkitURL!= null)
{
// Chrome允许以编程方式点击链接。
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.click();
}
else
{
// Firefox需要用户实际点击链接。
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
document.body.appendChild(downloadLink);
}

如果记事本不是什么大问题,那么您应该也可以打开这个blob在iframe中为.txt,然后右键单击并保存,如果您愿意的话。

编辑



<好的,这对我来说实际上是新的,所以我的一些旧信息不太正确。这里是从工作小提琴的JavaScript:

  var arrayOfStuff = []; 
arrayOfStuff.push(document.getElementById(name)。value +\\\
);
arrayOfStuff.push(document.getElementById(email).value);
arrayOfStuff.push(\\\
);
arrayOfStuff.push(document.getElementById(phone).value);
arrayOfStuff.push(\\\
);
arrayOfStuff.push(document.getElementById(comments).value);
arrayOfStuff.push(\\\
);
alert(arrayOfStuff);

var blob = new Blob(arrayOfStuff,{type:'text / plain'});

var link = document.getElementById(downloadLink);
link.download =details.txt;
link.href = window.URL.createObjectURL(blob);

小提琴在 http://jsfiddle.net/xHH46/2/



有几点教训:


  1. 如果您使用的是Firefox,则可以选择在记事本中立即打开.txt文件。然而,记事本并没有注意到换行符,不管它们是\ n还是\\\\,附加到直接字符串或单独添加,所以我建议使用写字板代替。或者,您可以保存该文件。
  2. 更重要的是,要知道您创建blob时显示的链接基于文本中的任何值。如果你没有默认值,你会得到一个空文件,因为所有的字段都是空的。 wordpress解决方案解决了这个问题(并在上周讨论了它的使用方法),但修复很难看。基本上,你必须点击一个按钮,然后这个按钮才会出现一个链接,并且该链接会给你一个好的文件。


Is there any link for exporting the datas to notepad? I have some fields like Name, Age, and WorkingStatus

These are text and textarea...

I want to insert this datas to the notepad.Is there any demos or code available?

解决方案

I don't know of any way to have the browser open notepad, but you can use HTML5 features to save a file as text, and then open it on your own inside notepad. Depending on the browser, you may need to trigger saving the file on the user side. Here's two references, which I'll summarize:

http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a-text-file-in-html5-using-javascrip/

http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side

Basically, you want to create and save a blob with your text. It should look something like this:

var arrayOfStuff = [];
arrayOfStuff.push("Name            Age              Working status");
arrayOfStuff.push("-----------------------------------------------");
arrayOfStuff.push(document.getElementById("name").value);
// etc
var blob = new Blob(arrayOfStuff, {type:'text/plain'});

// (the rest is copied directly from the wordpress link)
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
    // Chrome allows the link to be clicked programmatically.
    downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    downloadLink.click();
}
else
{
    // Firefox requires the user to actually click the link.
    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
    document.body.appendChild(downloadLink);
}

If notepad isn't a big deal, you should also be able to open this blob in an iframe as .txt, and then right-click and saveAs, if you prefer.

Edit

Ok, this was actually new for me to play with, so some of my older information wasn't quite right. Here's the javascript from the working fiddle:

var arrayOfStuff = [];
arrayOfStuff.push(document.getElementById("name").value + "\n");
arrayOfStuff.push(document.getElementById("email").value);
arrayOfStuff.push("\n");
arrayOfStuff.push(document.getElementById("phone").value);
arrayOfStuff.push("\n");
arrayOfStuff.push(document.getElementById("comments").value);
arrayOfStuff.push("\n");
alert(arrayOfStuff);

var blob = new Blob(arrayOfStuff, {type:'text/plain'});

var link = document.getElementById("downloadLink");
link.download = "details.txt";
link.href = window.URL.createObjectURL(blob);

The fiddle is at http://jsfiddle.net/xHH46/2/

There are a few lessons learned:

  1. If you're on firefox, it gives you the option to open the .txt immediately in Notepad. However, notepad isn't paying attention to the linefeeds, whether they're \n or \n\r, appended to the immediate string or added separately, so I'd recommend using Wordpad instead. Or, you can save the file.
  2. More importantly, realize that the link you display is based on whatever value is in the text when you create the blob. If you don't have defaults, you'll get an empty file, because all the fields are empty. The wordpress solution fixes this (and discusses using it within the past week), but the fix is ugly. Basically, you'd have to click on a button, and the button would then make a link appear, and that link would give you the good file.

这篇关于使用jquery导入到记事本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆