0

I am very unfamiliar with how caching works, and so I was wondering how I could pull a csv off a server the first time, but on subsequent refreshes use a cached csv file?

I know how to get the file from the server, and parse it via PapaParse. However, I do not know how to make that csv cached, or even how to access the cache via PapaParse.

Going further with this, how could I then check how long ago this csv file has been updated, and if it needs to be updated pull the csv off the server again? I understand there is a lastModified property in Javascript but it is unclear whether or not that works for cached files.

4

1 回答 1

0

您从中下载 CSV 文件的浏览器和服务器应该为您协商缓存;我可能会误解,但我不确定 Papa Parse 与此有什么关系。

对于上次修改日期,如果该文件恰好被用户<input type="file">在页面上选择了一个元素,您可以访问 File 对象并检查其lastModifiedlastModifiedDate属性(至少,它们存在于 Chrome 上)。

要获取要下载的文件的最后修改日期, MDN为您准备了一个示例:

function getHeaderTime () {
  alert(this.getResponseHeader("Last-Modified"));  /* A valid GMTString date or null */
}

var oReq = new XMLHttpRequest();
oReq.open("HEAD" /* use HEAD if you only need the headers! */, "yourpage.html", true);
oReq.onload = getHeaderTime;
oReq.send();

这只是做一个 HEAD 请求(只检查 last-modified 而不是获取整个文件,你需要的只是标题)并读取 Last-Modified 标题。

于 2014-11-24T02:15:24.050 回答