【问题标题】:DOS based printing through ASP.NET通过 ASP.NET 基于 DOS 的打印
【发布时间】:2009-04-25 06:31:44
【问题描述】:

我的情况是这样的:

我在服务器上以文本文件的形式生成报告,需要在点阵打印机上使用 DOS 模式打印。我想避免 Windows 打印,因为它太慢了。 ASP.NET 中有没有一种方法可以通过它执行基于 DOS 的打印,因为它最适合点阵打印机。我已经搜遍了网络,但找不到任何解决方案或指示。是否有任何机构有任何他们可能已经实施或偶然发现的指针/解决方案。

此应用程序是基于 Web 的应用程序。

谢谢。

【问题讨论】:

  • 您想从服务器还是客户端打印?我的意思是你想打印到安装在 Web 服务器或客户端的打印机上吗?对于前者,只需创建一个单独的 exe/batch 文件并让 ASP.NET 应用程序执行它,对于后者,您将需要某种 ActiveX 来实现这一点。
  • 如何让 ASP.nET 应用程序执行我感兴趣的批处理文件?

标签: asp.net printing


【解决方案1】:

如果我理解正确,一种选择是执行一个批处理文件,该文件将从 ASP.NET 进行实际打印。来自here:(显然可以省略一些将输出写入页面的代码)

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);


// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath); 

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;


// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();


// Close the io Streams;
sIn.Close(); 
sOut.Close();


// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

【讨论】:

  • 这是一些严重的黑客行为,伙计!做得好。我会把它扔进一个队列,然后想办法处理它。大声笑
【解决方案2】:

BobbyShaftoe 的回答是正确的。这是它的迂腐版本:

public static void CreateProcess(string strFilePath)
{
    // Create the ProcessInfo object
    var psi = new ProcessStartInfo("cmd.exe")
                  {
                      UseShellExecute = false,
                      RedirectStandardOutput = true,
                      RedirectStandardInput = true,
                      RedirectStandardError = true,
                      WorkingDirectory = "c:\\temp\\"
                  };

    // Start the process
    using (var proc = Process.Start(psi))
    {
        // Attach the in for writing
        var sIn = proc.StandardInput;

        using (var strm = File.OpenText(strFilePath))
        {
            // Write each line of the batch file to standard input
            while (strm.Peek() != -1)
            {
                sIn.WriteLine(strm.ReadLine());
            }
        }

        // Exit CMD.EXE

        sIn.WriteLine(String.Format("# {0} run successfully. Exiting", strFilePath));
        sIn.WriteLine("EXIT");

        // Read the sOut to a string.
        var results = proc.StandardOutput.ReadToEnd().Trim();

        // Write out the results.
        string fmtStdOut = "<font face=courier size=0>{0}</font>";
        this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多