简体   繁体   English

如何通过管道将stdout发送到Mono Apache MVC2应用程序中的浏览器

[英]how to pipe stdout to browser in Mono Apache MVC2 application

Code below is used to save PostgreSql database backup from browser in Apache Mono MVC2 web application in Linux. 以下代码用于在Linux的Apache Mono MVC2 Web应用程序中从浏览器保存PostgreSql数据库备份。

It takes long time for backup to finish before file transfer begins. 文件传输开始之前,备份需要很长时间才能完成。 pg_dump can write to standard output instead of file. pg_dump可以写入标准输出而不是文件。 How to force controller to pipe standard output to browser without creating temporary file ? 如何在不创建临时文件的情况下强制控制器将标准输出传递给浏览器? Or how to show some progress indicator to user ? 还是如何向用户显示一些进度指示器?

[Authorize] 
public class BackupController : ControllerBase 
 { 
   [AcceptVerbs(HttpVerbs.Get)] 
   public ActionResult Backup() 
   { 
            var pinfo = new ProcessStartInfo(); 
            var fn = "temp.backup"; 
            pinfo.Arguments = " -f \"" + fn + "\" -Fc -h \"" + "myserver" + "\" -U \"" +                 "postgres" + " \"" + "mydb" + "\""; 
            pinfo.FileName = "/usr/lib/pgsql/pg_dump"; 
            pinfo.UseShellExecute = false; 
            using (var process = new Process()) 
            { 
                process.EnableRaisingEvents = true; 
                process.StartInfo = pinfo; 
                process.Start(); 
                while (!process.HasExited) 
                    Thread.Sleep(2000); 
                process.WaitForExit(); 
                if (process.ExitCode!=0) 
                  throw new Exception("error"); 
                process.Close(); 
            } 
            Response.ClearContent(); 
            Response.WriteFile(fn); 
            Response.End(); 
            System.IO.File.Delete(fn); 
            return null; 
        } 
} 

update 更新

I tried code below according to answer. 我根据答案尝试了以下代码。 Backup copy saved from browser causes pg_restore to crash. 从浏览器保存的备份副本导致pg_restore崩溃。 How to use binary write or something other to create correct backup ? 如何使用二进制写或其他方法创建正确的备份? Also browser prompts for save only after pg_dump has finished. 另外,浏览器仅在pg_dump完成后提示保存。 How to implement pipe so that data is transferred over internet if pd_dump is working ? 如果pd_dump工作正常,如何实现管道以便通过Internet传输数据?

[Authorize]
public class BackupController : ControllerBase
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Backup()
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", string.Format("attachment; filename=\"backup.backup\"");
        Response.ContentType = "application/backup";
        using (var process = new Process())
        {
            process.StartInfo.Arguments = " -ib -Z6 -Fc -h \"server\"";
            process.StartInfo.FileName = "/usr/lib/pgsql/pg_dump";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            Server.ScriptTimeout = 86400;
            process.Start();
            while (!process.HasExited)
            {
                var b = process.StandardOutput.ReadToEnd();
                Response.Write(b);
                Thread.Sleep(2000);
            }
            process.WaitForExit();
            if (process.ExitCode != 0)
            {
                return new ContentResult()
                {
                    Content = "Error " + process.ExitCode.ToString()
                };
            }
            var b2 = process.StandardOutput.ReadToEnd();
            Response.Write(b2);
            process.Close();
            Response.End();
            return null;
        }
    }

您可以使用RedirectStandardOutput将标准输出重定向到流,然后从Process.StandardOutput中读取内容,从而使您可以显示一些进度(与AJAX左右结合,您可能很了解)。

Maybe write the output stream to a temp buffer (file or memory stream) while another thread pipes to the browser with singnalR? 也许将输出流写入临时缓冲区(文件或内存流),而另一个线程通过singnalR传递给浏览器? Here's a good video demonstrating signalR: http://channel9.msdn.com/Shows/Web+Camps+TV/Damian-Edwards-and-David-Fowler-Demonstrate-SignalR 这是一个很好的视频演示signalR: http ://channel9.msdn.com/Shows/Web+Camps+TV/Damian-Edwards-and-David-Fowler-Demonstrate-SignalR

It would allow you to send data to the browser in real time as the stream contents change. 当流内容更改时,它将允许您将数据实时发送到浏览器。 Also, you say there was corruption, I only say this cause it's bit me more than once, make sure you serialized and deserialize your output with the same character encoding. 另外,您说有损坏,我只是说这是因为不止一次,请确保使用相同的字符编码对输出进行序列化和反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM