如何阅读使用javascript文件的内容是什么?文件、内容、javascript

2023-09-11 01:05:30 作者:可爱又迷人的小妖精吖

我有一个输入类型=文件按钮。当我选择一个文件,我要读使用JavaScript文件的内容。是否有可能读取/获得使用JavaScript和AJAX选定的文件的内容?

I have an input type="file" button. After I choose a file, I have to read the contents of the file using javascript. Is it possible to read/get contents of a chosen file using javascript or ajax?

推荐答案

使用AJAX其可以读取上传的文件,但与纯JavaScript它不是可能的,因为JavaScript的工作在客户端而不是断绝身边。

With AJAX its possible to read uploaded file but with pure javascript its not possible because javascript works on client side not on sever side.

如果你要使用jQuery比Ajax调用可能是这样

if you are going to use jquery than Ajax call may be like this

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});