如何使用Javascript读取文本文件 [英] how to read a text file using Javascript

查看:84
本文介绍了如何使用Javascript读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从用户那里获取一个文本文件,然后使用将内容存储在db中的ajax将文件发布回浏览器,然后使用Jquery发布响应将内容显示回用户页面. 现在我想要这样的东西.

I am taking a text file from user and then posting that file back to the browser using ajax storing the content in db and then showing the content back to user page using Jquery post response. Now i want to something like this..

使用javascript从用户计算机读取文本文件.显示内容,当他提交页面时,我将保存这些值.

Read the text file from the user computer using javascript. Display the content and when he submits the page I will save the values.

推荐答案

出于安全原因,无法在纯JS中完成.您需要让用户将文件上传到服务器,然后通过Ajax取回内容.

Can't be done in pure JS for security reasons. You would need to have the user upload the file to your server, and fetch the contents back through Ajax.

如果使用Flash或Java,则应该能够直接访问该文件.如果您讲Flash/Actionsript,也许 SWFUpload 的源代码(尤其是新的客户端调整大小功能)可以作为灵感.

If you use Flash or Java, you should be able to gain direct access to the file. If you speak Flash/Actionsript, maybe SWFUpload's source code (especially the new client-side resizing functions) can serve as an inspiration.

更新:该博客条目应该会有所帮助. 使用Flash Player 10读写本地文件

Update: This blog entry should help. Read and write local files with Flash Player 10

更新:要详细说明上传并提取",如果在IFRAME中进行上传,甚至可以让上传脚本仅输出文本文件的内容.由于iframe属于您的域,因此上传完成后,您将可以通过JavaScript检索其内容.只要发送content-type: application/text,它就可以免受任何恶意攻击.

Update: To elaborate on the "upload and fetch" thing, if you do the uploading in an IFRAME, you could even have the upload script simply output the text file's contents. Because the iframe belongs to your domain, you will be able to retrieve its contents via JavaScript when the upload has finished. As long as you send a content-type: application/text it should be fairly safe from any malicious attacks.

这篇关于如何使用Javascript读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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