Javascript 将内容写入浏览器中的文件-fs.writeFileSync-
我的查询 如何将某些内容写入浏览器中支持的文件 我尝试过的 我使用Javascript 将内容写入浏览器中的文件-fs.writeFileSync-,javascript,node.js,browser,Javascript,Node.js,Browser,我的查询 如何将某些内容写入浏览器中支持的文件 我尝试过的 我使用brfs 这捆绑了一个处理文件读取的输出,并用文件中的实际数据添加\替换回来 但是在处理完文件内容后,我需要用另一个名称来编写文件。 为此,我有以下声明 fileStream.writeFileSync(fileName, doc); 但是上面的fileStream不会被brfs操作(只有fileStream.readFileSync('file')会被brfs更新) 请帮我写回文件以便浏览 更新 我对NodeJS完全陌生。下面
brfs
这捆绑了一个处理文件读取的输出,并用文件中的实际数据添加\替换回来
但是在处理完文件内容后,我需要用另一个名称来编写文件。
为此,我有以下声明
fileStream.writeFileSync(fileName, doc);
但是上面的fileStream不会被brfs操作(只有fileStream.readFileSync('file')
会被brfs更新)
请帮我写回文件以便浏览
更新
我对NodeJS完全陌生。下面我放置了代码-方法名ProcessFile
-它工作正常。
但是,当我使用此方法在浏览器上执行功能时,我遇到了一些问题
当我执行命令时,browserify-t brfs main.js-o bundle.js
在bundle.js中,方法ProcessFile
(放在下面)转换如下:
ProcessFile
:
函数ProcessFile(文件路径、targetColor、fillColorMode)
{
var fileStream=require('fs');//STT\u fs
var xmldoc=require('xmldoc');
var stringMethods=require('string');
//从给定文件路径加载svg文件。
var filePathStr=fileStream.readFileSync('E:\\NodeJS\\macPump.svg').toString();//STT\u FS\u READ
//将svg文件转换为xml文档
var doc=新的xmldoc.XmlDocument(filePathStr);
//从基本SVG文件中收集组
var groupNodes=doc.childrenNamed('g');
var-sourceAttr;
var fillAttr;
//如果这些组在svg文件中不可用,则将所有元素设置为子元素
如果(groupNodes.length==0){groupNodes=doc.children;}
//如果目标颜色模式为空,则将目标颜色设置为透明
如果(fillColorMode.toLowerCase()==“空心”){targetColor='transparent';}
//在此处迭代每个groupNodes元素以更改颜色
对于(var groupCount=0;j=groupNodes.length,groupCount 对于(var styleCount=0;k=styleElement.length,styleCount你能给我们看一些代码吗?一个错误stacktrace?我已经更新了我的问题。我得到的错误是==>SCRIPT5009:“fileStream”未定义-----当我在浏览器中执行脚本时-----它在标记为STT_FS_WRITE-----的语句中,因为在final bundle.js中声明文件estream=require('fs')已删除。请告诉我如何通过将更改后的colr\shade写入另一个文件来获得写入文件内容的结果。还请告诉我是否可以通过任何其他方式实现它===>如ajax调用node.js服务器,以便从浏览器实现该功能。
function ProcessFile(filePath,targetColor,fillColorMode)
{
var fileStream = require('fs'); //STT_FS
var xmldoc = require('xmldoc');
var stringMethods = require('string');
// Load the svg File from the givenfile path.
var filePathStr = fileStream.readFileSync('E:\\NodeJS\\macPump.svg').toString();//STT_FS_READ
//convert svg file to xml document
var doc = new xmldoc.XmlDocument(filePathStr);
//Collect the Groups from base SVG File
var groupNodes = doc.childrenNamed('g');
var sourceAttr ;
var fillAttr;
//If the groups are not available in svg file, then set all elements as children
if(groupNodes.length===0) { groupNodes=doc.children; }
//if target color mode is hollow then set target color to transparent
if(fillColorMode.toLowerCase()==="hollow") {targetColor='transparent';}
//iterate each groupNodes element here to change the color
for (var groupCount = 0; j = groupNodes.length, groupCount < j; groupCount++)
{
var styleElement =groupNodes[groupCount].children;
//if the groupNodes element does not contains sub node then get the style attribute value directly .
if(styleElement.length===0)
{
styleElement=groupNodes[groupCount];
fillAttr=styleElement.attr['style'];
if(fillColorMode.toLowerCase()!=="original")
{
// get updated target color here for basic type svg files
styleElement.attr['style']=GetFillColor(fillAttr,targetColor,fillColorMode);
}
}//end of if
else
{
for(var styleCount=0; k=styleElement.length,styleCount<k;styleCount++)
{
fillAttr=styleElement[styleCount].attr['fill'];
if(fillColorMode.toLowerCase()!=="original")
{
// get fill element color here
var fillElementColor=ChangeFillElementColor(fillAttr,targetColor,fillColorMode);
if(fillElementColor != undefined && fillElementColor != 'none')
{
styleElement[styleCount].attr['fill']='#'+fillElementColor;
}
}var styleAtt=styleElement[styleCount].children;
for(var t=0; l=styleAtt.length,t<l;t++)
{
sourceAttr= styleAtt[t].attr['style'];
var convertedStyleColor;
if(sourceAttr != 'none')
{
if(fillColorMode.toLowerCase()==="hollow")
{
//get here transparent color
convertedStyleColor=ConvertStringToColor(targetColor);
}
else
{
// Extract the style value and apply color accordingly gievn in tag color
sourceAttr= GetColor(sourceAttr)
convertedStyleColor= GetUpdatedTargetColor(sourceAttr,targetColor,fillColorMode);
}
if(fillColorMode.toLowerCase()!=="original")
{
styleAtt[t].attr['style']='stop-color:#'+convertedStyleColor;
}
}
}//end of for
}//end of for
} //end of else
}//end of for
//get filename here
var newFileName= [] ;
newFileName[0]=filePath;
var arr=newFileName[0].split('\\');
//ger the file name from given path
var fileName = 'ReColored' + arr[arr.length-1];
//save here new svg file with target color
fileStream.writeFileSync(fileName, doc);//STT_FS_WRITE
}