简体   繁体   English

如何将 FileReader 更改为 url

[英]how to change FileReader to url


I want to read xlsx files using js.我想使用 js 读取 xlsx 文件。
This is the code I have:这是我的代码:

$(document).ready(function() {

$('#input-excel').change(function(e) {
  var reader = new FileReader();
  reader.readAsArrayBuffer(e.target.files[0]);

  reader.onload = function(e) {
    console.log(reader.result);
    var data = new Uint8Array(reader.result);
    console.log(data);
    var wb = XLSX.read(data,{type:'array'});
    console.log(wb);
  }
});
});

It gives me the xlsx in an array object, exactly what I need.它给了我一个数组对象中的 xlsx,正是我需要的。

But I don't want the user to load the file,但我不希望用户加载文件,
I want the file to be loaded from a URL.我希望从 URL 加载文件。

Does someone know how to do it?有人知道怎么做吗?
Thank You谢谢你

Let's say you have the same XLS file, you can use ajax to load it.假设您有相同的 XLS 文件,您可以使用 ajax 加载它。 Almost similar:几乎相似:

  $.ajax({
    url: URL_TO_FILE
  }).done(function(data) {
    console.log(data);
    var wb = XLSX.read(data,{type:'array'});
    console.log(wb);
  });
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


JSONParser parser = new JSONParser();
ObjectMapper mapper = new ObjectMapper();

try {
    
    Object obj = parser.parse(new FileReader("/home/sahan/Desktop/data.json"));
    ObjectNode objNode = mapper.convertValue(obj, ObjectNode.class);
    System.out.println(objNode);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

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

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