Code below is used to download backup from browser created by pg_dump in MVC2 application. If pg_dump crashes, incomplete file is downloaded without any notice.
How to detect crash and send message to browser so that downloaded file is incomplete or OK? Maybe Exited event with jquery in browser can used as shown in code below.
[Authorize]
public class BackupController : ControllerBase {
[AcceptVerbs(HttpVerbs.Get)]
public FileStreamResult Backup() {
var process = new Process();
process.StartInfo.Arguments = "-ib -Z6 -Fc -h 45.878.78.76";
process.StartInfo.FileName = "c:\\Program Files\\PostgreSql\\9.1\\bin\\pg_dump.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
// todo: how to send and show exit code and error info in browser
};
process.Start();
return new FileStreamResult(process.StandardOutput.BaseStream,
"application/mybackup");
}
}
In my opinion, the best is to start the process and make it log to a file. Then read from the file( open file read with no lock) and show the ouptout to the user . ( You can do also browser refresh meta tag or ajax to poll the server for modifications)
kobruleht
Member
589 Points
466 Posts
How to detect broken filestream download
Mar 25, 2012 06:59 AM|LINK
Code below is used to download backup from browser created by pg_dump in MVC2 application. If pg_dump crashes, incomplete file is downloaded without any notice.
How to detect crash and send message to browser so that downloaded file is incomplete or OK? Maybe Exited event with jquery in browser can used as shown in code below.
[Authorize] public class BackupController : ControllerBase { [AcceptVerbs(HttpVerbs.Get)] public FileStreamResult Backup() { var process = new Process(); process.StartInfo.Arguments = "-ib -Z6 -Fc -h 45.878.78.76"; process.StartInfo.FileName = "c:\\Program Files\\PostgreSql\\9.1\\bin\\pg_dump.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true;Server.ScriptTimeout = 8 * 60 * 60; // 8 hours process.EnableRaisingEvents = true; process.Exited += (sender, e) => { TempData["ExitCode"] = process.ExitCode;// todo: how to send and show exit code and error info in browser }; process.Start(); return new FileStreamResult(process.StandardOutput.BaseStream, "application/mybackup"); } }ignatandrei
All-Star
137665 Points
22145 Posts
Moderator
MVP
Re: How to detect broken filestream download
Mar 25, 2012 07:21 AM|LINK
because it takes that much time
In my opinion, the best is to start the process and make it log to a file. Then read from the file( open file read with no lock) and show the ouptout to the user . ( You can do also browser refresh meta tag or ajax to poll the server for modifications)
kobruleht
Member
589 Points
466 Posts
Re: How to detect broken filestream download
Mar 25, 2012 07:44 AM|LINK
I understand from this:
1. Clicking in download link uses window.open to redirect file download to other window.
2. In former window jquery ajax call periodically checks for Session["ExitCode"] and if this appears in session data, shows this for user.
Is this best solution ?
process.StartInfo.RedirectStandardError = true causes deadlock so it cannot used.
ignatandrei
All-Star
137665 Points
22145 Posts
Moderator
MVP
Re: How to detect broken filestream download
Mar 25, 2012 07:54 AM|LINK
Right.