2017-07-08 63 views
0

我们如何从项目文件路径中将文本文件的内容复制到JavaScript中的字符串变量。Javascript:从项目文件夹中读取文本文件到字符串

我使用下面的代码,但它给出了“访问被拒绝”错误。

var sHTML = ""; 
var rawFile = new XMLHttpRequest(); 
rawFile.open("GET", "file://~/Templates/NewGridTemplate.txt", true); 
     sHTML = rawFile.responseText; 
+0

这可能有助于https://stackoverflow.com/questions/13709482/how-to - 读取文本文件,在JavaScript的 – xxLITxx

回答

0

阅读C#代码的HTML模板,然后将它发送给你的HTML代码,并分配给innerHTML的

using (StreamReader sr = new StreamReader(@"D:\Template.html")){ 
    // Read the stream to a string, and write the string to the console. 
    obj.svar = sr.ReadToEnd().Replace(Environment.NewLine, " "); 
    //Console.WriteLine(line); 
    } 
相关问题