C# 使用phantomjs和asp.net web api拍摄网页截图
我正在尝试截图的网址,并保存在我的服务器截图。我正在使用phantomjs拍摄截图。 我在网上找到了一些示例代码,但它在我的应用程序中不起作用 它似乎陷入了一种过时的方法 但是,当我从cmd.exe运行phantomjs时,它工作得很好C# 使用phantomjs和asp.net web api拍摄网页截图,c#,asp.net,asp.net-web-api,phantomjs,C#,Asp.net,Asp.net Web Api,Phantomjs,我正在尝试截图的网址,并保存在我的服务器截图。我正在使用phantomjs拍摄截图。 我在网上找到了一些示例代码,但它在我的应用程序中不起作用 它似乎陷入了一种过时的方法 但是,当我从cmd.exe运行phantomjs时,它工作得很好 phantomjs rasterize.js http://www.google.com google.png 在我的项目中,我有一个名为Phantomjs的文件夹,其中包含Phantomjs.exe和rasterize.js脚本 你知道我做错了什么吗 pri
phantomjs rasterize.js http://www.google.com google.png
在我的项目中,我有一个名为Phantomjs的文件夹,其中包含Phantomjs.exe和rasterize.js脚本
你知道我做错了什么吗
private static void GrabUrl(String url)
{
string serverPath = HttpContext.Current.Server.MapPath("~/Phantomjs/");
string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".png";
new Thread(new ParameterizedThreadStart(x =>
{
ExecuteCommand("cd " + serverPath + @" & phantomjs rasterize.js " + url + " " + filename + @" ""A4""");
})).Start();
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Phantomjs/"), filename);
var stream = new MemoryStream();
byte[] bytes = DoWhile(filePath);
}
private void ExecuteCommand(string Command)
{
try
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
Process = Process.Start(ProcessInfo);
}
catch { }
}
private byte[] DoWhile(string filePath)
{
byte[] bytes = new byte[0];
bool fail = true;
while (fail)
{
try
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
}
fail = false;
}
catch
{
Thread.Sleep(1000);
}
}
System.IO.File.Delete(filePath);
return bytes;
}
删除Execute命令中的try/catch块没有任何作用。我确实有一个例外:
Exception:Thrown: "Could not find file 'C:\ ~\Web API\Phantomjs\28042014_042756.png'." (System.IO.FileNotFoundException)
A System.IO.FileNotFoundException was thrown: "Could not find file 'C:\~\Web API\Phantomjs\28042014_042756.png'."
Time: 28-Apr-14 16:28:00
Thread:Worker Thread[2068]
它是否生成了实际的屏幕截图文件?当我从cmd运行它时,它会创建它。根据上面的代码,它不会创建任何东西。只是被困在DoWhile方法中…取出ExecuteCommand上的空catch块,看看会发生什么。就像@RobH提到的,这意味着问题出在ExecuteCommand上:正如一些需要考虑的架构/安全性一样,你真的希望你的网站在一个能够在服务器上执行任意命令的帐户下运行吗?