简体   繁体   English

如何从javascript中的输入文件中读取文本?

[英]how to read text from input file in javascript?

I have a text file i want to read in my html page,both are in the same directory and i am not running a server. 我有一个文本文件,我想在我的html页面中阅读,两者都在同一目录中,并且我没有在运行服务器。 I intend that my users use the script offline(basically text manipulation based on expressions and preserving new line characters) . 我希望我的用户脱机使用脚本(基本上是基于表达式的文本操作并保留换行符)。

  1. I tried ajax call but mostly cross domain origin problem occured and i know most of the users will have this security tighened up in many browsers , so its not of use to circumvent this in only in my browser. 我尝试了ajax调用,但是大多数跨域起源问题都发生了,并且我知道大多数用户的安全性在许多浏览器中都会被削弱,因此仅在我的浏览器中就没有用它来规避此安全性。

  2. I want to support many browsers including old browsers as in IE7,8 etc which do not support HTML5 filereader.for the same reason reading using filesystemobject or activex is not good. 我想支持许多浏览器,包括IE7,8等不支持HTML5 filereader的旧浏览器。出于同样的原因,使用filesystemobject或activex读取效果也不佳。

  3. Reading the file after user select it as input , is this possible?Otherwise i would have no option then using other technologies like php,java etc which may expect my user to setup these. 在用户选择它作为输入之后读取文件是否可行?否则我将没有选择,然后使用其他技术(例如php,java等)来期望我的用户进行设置。

Please excuse me if i am repeating this but i am a beginner web developer. 如果我要重复此步骤,请原谅,但我是初学者Web开发人员。 I know that reading local files via javascript is not possible but is there any other way? 我知道无法通过javascript读取本地文件,但是还有其他方法吗?

If you can't support FileReader, then the answer is pretty much no (at least, if you want to support a large range of browsers rather than rely on convenient feature x of browser y). 如果您不支持FileReader,则答案几乎是“否”(至少,如果您想支持各种浏览器,而不是依赖于浏览器y的便捷功能x)。 Unless : you indeed increase the requirements for running the application and get some sort of local server going (for instance node.js, Apache, TomCat, etc. but like you said this will greatly increase the requirements and become cumbersome for users). 除非:您确实增加了运行应用程序的需求并获得了某种本地服务器的支持(例如,node.js,Apache,TomCat等,但是就像您所说的那样,这将大大增加需求并给用户带来麻烦)。

You could also rethink what it is you're trying to achieve. 您还可以重新考虑您要实现的目标。 What are the contents of the file you want to read ? 您要阅读的文件内容是什么? Can't these contents be part of the HTML file you're serving to your users (ie a large JSON Object inside a script-tag ?) 这些内容不能成为您提供给用户的HTML文件的一部分(例如,脚本标记中的大JSON对象吗?)

On possibility of using node.js: 关于使用node.js的可能性:

Node.js is quite easy to install and assuming you are requiring from your users to install it, you can use it as a local server, which is a nodejs script of about two lines in size :). Node.js的安装非常容易,并且假设您需要用户安装它,则可以将其用作本地服务器,这是一个大小约为两行的nodejs脚本:)。 Running it locally would also omit the need to upload anything anywhere as you can directly read from the file system using the fs-object (see sitepoint.com/accessing-the-file-system-in-node-js). 在本地运行它也将不需要上传任何内容,因为您可以使用fs-object直接从文件系统读取文件(请参阅sitepoint.com/accessing-the-file-system-in-node-js)。

STILL: from both a design and ease-of-use-point of view you might want to resort to using either another technology, or include the text file content inside the HMTL file 仍然:从设计和易用性的角度来看,您可能想求助于使用另一种技术,或者将文本文件内容包含在HMTL文件中

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

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