Open external program via mvc5

Asked

Viewed 479 times

0

I’m having a hard time doing an operation. I have a console application that opens a PDF. This console takes the name of Servico.exe, although it has the service name, it is an exe and not a service. Opening the PDF is ok. I created a MVC5 project. I used the ADO .Net Entity Framework which led Morrison to say that this was a bad approach, but since it is only a POC I went ahead and then I see another approach. My difficulty is opening the program that calls the report from within the controller or view. Below is the controller that should do this. This controller was generated by the VS2013 Wizard. I cannot mount an action or method that does this.

public class AppealReportController : Controller
    {
        private ReportDBContext db = new ReportDBContext();

        // GET: AppealReport
        public async Task<ActionResult> Index()
        {
            var pOC_SOLIC_RELATORIO = db.POC_SOLIC_RELATORIO.Include(p => p.POC_RELATORIO);
            return View(await pOC_SOLIC_RELATORIO.ToListAsync());
        }

        // GET: AppealReport/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Create
        public ActionResult Create()
        {
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO");
            return View();
        }

        // POST: AppealReport/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pOC_SOLIC_RELATORIO).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }

        // GET: AppealReport/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            if (pOC_SOLIC_RELATORIO == null)
            {
                return HttpNotFound();
            }
            return View(pOC_SOLIC_RELATORIO);
        }

        // POST: AppealReport/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO = await db.POC_SOLIC_RELATORIO.FindAsync(id);
            db.POC_SOLIC_RELATORIO.Remove(pOC_SOLIC_RELATORIO);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

I tried that and it didn’t work:

public void openApplication()
        {
            System.Diagnostics.Process.Start("C:\\Projetos\\Servicos\\bin\\Servicos.exe");
        }

And I made that call:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID_SOLIC_RELATORIO,ID_RELATORIO,ID_USUARIO,DT_SOLICITACAO,DT_AGENDAMENTO,DT_GERACAO,BL_RELATORIO")] POC_SOLIC_RELATORIO pOC_SOLIC_RELATORIO)
        {
            if (ModelState.IsValid)
            {
                db.POC_SOLIC_RELATORIO.Add(pOC_SOLIC_RELATORIO);
                openApplication();//Aqui não funfa
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ID_RELATORIO = new SelectList(db.POC_RELATORIO, "ID_RELATORIO", "NM_RELATORIO", pOC_SOLIC_RELATORIO.ID_RELATORIO);
            return View(pOC_SOLIC_RELATORIO);
        }
  • As the question became broad, I made an edit and changed the title and content only for the opening of an external executable program on mvc.

  • There is a big problem of approach there. An ASP.NET MVC application is a web application, so it is not made to open executables, even if they are local. A classic of this is the package Rotating, which generates a PDF by calling an executable and does not work in Windows Azure web instances, for example. Anyway, I’ll answer your question.

1 answer

2


Basically, what you do is the same as the Rotating do, then I’ll play the method invoking the service with some changes to your case, reads its output and converts it into an array of bytes, which is the content of the PDF:

    protected static byte[] AbrirExecutavelExtrairPdf()
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = @"C:\Projetos\Servicos\bin\Servicos.exe",
                Arguments = "",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                WorkingDirectory = @"C:\Projetos\Servicos\bin",
                CreateNoWindow = true
            }
        };
        proc.Start();

        using (var ms = new MemoryStream())
        {
            using (var sOut = proc.StandardOutput.BaseStream)
            {
                byte[] buffer = new byte[4096];
                int read;

                while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
            }

            string error = proc.StandardError.ReadToEnd();

            if (ms.Length == 0)
            {
                throw new Exception(error);
            }

            proc.WaitForExit();

            return ms.ToArray();
        }
    }
  • I just don’t understand the parameters in the Open method executavelextrairpdf. They are not used anywhere.

  • Actually they don’t need it. I used the method I pointed out to make a simpler one for you and forgot to remove the parameters. Now it’s ok.

  • 1

    It worked to open the exe, thanks. I will mark your reply.

  • Gypsy, how do I open this generated file? In the bank there was a number and I do not know how to open to see if it is the report or if it was a mistake.

  • Converting the byte array into a file. It may be the case that you have to ask another question.

  • Okay, I’ll burn some neurons and if I get nothing, I’ll open another post.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.