简体   繁体   English

与文件交互的最佳实践 (Javascript)

[英]Best Practice to interact with files (Javascript)

I want to upload a JSON file to my Server (I use NodeJS and 'http' Module as Web Server).我想将 JSON 文件上传到我的服务器(我使用 NodeJS 和“http”模块作为 Web 服务器)。 With the NPM module 'formidable', I was able to receive that file from the user and save it on my server.使用 NPM 模块“强大”,我能够从用户那里接收该文件并将其保存在我的服务器上。

Now I want to use informations from that file and show a statistical chart on the website.现在我想使用该文件中的信息并在网站上显示统计图表。 The problem is that I dont know how to embed that file, so that I can use these informations to create a chart on the website because NodeJS is serverside and I can't use modules like 'fs' for the client side code.问题是我不知道如何嵌入该文件,以便我可以使用这些信息在网站上创建图表,因为 NodeJS 是服务器端,我不能将像“fs”这样的模块用于客户端代码。 I tried WebPack, so that I am able to use 'fs' but it didn't work.我尝试了 WebPack,所以我可以使用“fs”,但它没有用。

What is the best way to realize this?实现这一点的最佳方法是什么?

Could you read the file into a string on your server using php, and then bring it in on the client side with JQuery?您可以使用 php 将文件读入服务器上的字符串,然后使用 JQuery 将其引入客户端吗? You'll need to go download, and include JQuery on your page, like this:您需要下载并在您的页面上包含 JQuery,如下所示:

<script src="jquery-3.2.1.js"; type="text/javascript"></script>

So something like this on your server:所以在你的服务器上是这样的:

fileread.php文件读取.php

<?php
$myinfo = file_get_contents('http://www.example.com/myfile.txt');
echo $myinfo;
?>

And on the webpage/client side, read in using JQuery:在网页/客户端,使用 JQuery 读入:

var path="http://www.example.com/fileread.php"
    $.get(path, function(data){
    content= data;
    alert(content);
    //then do whatever you need to with content...
    };

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

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