如何将js文件转换为缩小版本? [英] How to convert js files into minified version ?
本文介绍了如何将js文件转换为缩小版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hii,
我想创建一个Windows应用程序,我将上传我的js文件。并且它们应该被转换为缩小版本到那里的某个不同的位置。
Hii ,
I want to create one windows application in which i will upload my js files. and they should get converted into minified version of there into some different location.
推荐答案
创建方法
Create a Method
static string MiniFyJS(string inputFilePath, string outFilePath)
{
string _result = string.Empty;
if (!string.IsNullOrEmpty(inputFilePath) && !string.IsNullOrEmpty(outFilePath))
{
using (var sw = new StreamWriter(outFilePath, false))
{
string source = String.Empty;
try
{
source = File.ReadAllText(inputFilePath);
}
catch (IOException)
{
_result = inputFilePath + "File was not found.";
}
sw.WriteLine((new Minifier()).MinifyJavaScript(source));
sw.Flush();
_result = "Success";
}
}
else
{
_result = "Usage: jsmin <file1[,file2,fileN]> <outfile>";
}
return _result;
}
按钮点击事件调用此方法
Call this method on button click event
string inputFile = @"D:\JSFiles\calander.js";
string outFile = @"D:\MinifiedJSFiles\calander.min.js";
string result = MiniFyJS(inputFile, outFile);
检查一下回答,它可能对你有帮助,
如何使用C#压缩Javascript文件?
Hi, Check this answer, it may be helpful to you,
How to compress Javascript files using C#?
您必须使用AJAXMINIFY dll Microsoft Ajax Minifier [ ^ ]
使用以上DLL你有调用方法如
You have to use AJAXMINIFY dll Microsoft Ajax Minifier[^]
Using above DLL you have call method like
(new Minifier()).MinifyJavaScript(source);
它将返回缩小的脚本
您可以将它用作
It will return the minified script
You can use it as
static void Main(string[] args)
{
if (args == null || (args != null && args.Length < 2))
{
Console.WriteLine("Usage: jsmin <file1[,file2,fileN]> <outfile>");
return;
}
var argc = args.Length;
if (File.Exists(args[argc - 1]))
{
if (File.GetAttributes(args[argc - 1]) == FileAttributes.ReadOnly)
File.SetAttributes(args[argc - 1], FileAttributes.Normal);
}
using (var sw = new StreamWriter(args[argc - 1]))
{
for (var i = 0; i < argc - 1; ++i)
{
string source = String.Empty;
try
{
source = File.ReadAllText(args[i]);
}
catch (IOException)
{
Console.WriteLine("File \"{0}\" was not found.", args[i]);
continue;
}
sw.WriteLine((new Minifier()).MinifyJavaScript(source));
}
sw.Flush();
}
}
传递参数的语法
Syntax to pass parameter
jsmin <file1[,file2,fileN]> <outfile>
传递命令行参数,例如
Pass command line argument like
jsmin jquery-1.4.4.js jquery.min.js
这篇关于如何将js文件转换为缩小版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文