0
  public ActionResult Index(int id, string name)
  {
        var model = new ITViewModel
        {
           Packages = _Repository.GetDeployedPackages(id)
        };
        return View(model);
  }

 [HttpPost]
 public ActionResult GeneratePackage(ITViewModel model)
    {
    _Repository.SavePackage(model);

    //Generate Zip file Package
    //Get file template  in archiveStream
            Response.Clear();
            Response.ContentType = "application/zip";
            Response.AppendHeader("content-disposition", "attachment; filename="testzipPackage");
            Response.CacheControl = "Private";
            Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
            Response.Buffer = true;
            var writeBuffer = new byte[4096];

           var count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);
            while (count > 0)
            {
                Response.OutputStream.Write(writeBuffer, 0, count);
                count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);

           }
    model.Packages = _Repository.GetDeployedPackages(model.id) //get the correct package list with the one tht we just saved on this ActionResult

    return View("Index",model);
    }

    //Index
    @model  ITViewModel
    @using (Html.BeginForm("GeneratePackage", "Integration", FormMethod.Post)
{
    //some input form 
}

<table>
 @foreach (var package in Model.Packages)
            {
<tr>
<td>
        @package.Name
</td>
</tr>
}
</table>

I am able to download the zip file correctly. In the debugger I also see the Package list with the newly added element. But the View on Post is not getting refreshed. I mean the table on the Index doesn’t refresh with the new model element. Even the document.ready is not getting called once return View("Index",model) is fired.

 I have tried ModelState.Clear(). It didn't work.
Scorpio
  • 1,151
  • 1
  • 19
  • 37
  • 2
    You cannot have an HTTP response do two things at once. The content is either `text/html` (for a view) or `application/zip` (for a download) but it can't be both. You're going to need to do something else, like return the view and then kick off the download via javascript. – Paul Abbott May 17 '17 at 17:13
  • @PaulAbbott wat is the work around for this? I need the users to download the zip. But I also want them to see the updated list.. Can I force the page to refresh after sedning the zip – Scorpio May 17 '17 at 17:16
  • The workaround is to return the view and then kick off the download via javascript. You will need to make a second controller action that downloads the file and call that via javascript. You cannot download first and then refresh the page because the download is just a file transfer and there is no way to include "instructions" to do anything else after the download is complete. – Paul Abbott May 17 '17 at 17:21

1 Answers1

1

You cannot return two different responses from a single HTTP request.

Here you are writing the response:

Response.OutputStream.Write(writeBuffer, 0, count);

Anything you do after that is not handled by the server or the client.

Your web browser is downloading the file and than just stays on the same page. That's absolutely normal.

If you want to refresh the page you may need to do it using JavaScript client-side.

Here it is a small example using jQuery assuming myForm as your form id:

$('#myForm').submit(function() {
    setTimeout(function () {
        window.location.reload();
    }, 1000); // use a timeout as big as you need
});

You may also need to add target="_blank" to your form tag.

Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
  • I tried tht in document ready but its just not calling any script on the Index page on submit.. How can I force the page refresh on document download? – Scorpio May 17 '17 at 17:19
  • Attach your refresh logic on form submit, [like this](http://stackoverflow.com/questions/18920651/how-can-i-refresh-a-form-page-after-the-form-submits-to-blank) *OR* invert your logic and return a view with a link to your file to be downloaded (as Paul Abbott suggested). – Federico Dipuma May 17 '17 at 17:22
  • Are u suggesting $('#myForm').submit(function() { location.reload(); } to bind the submit event ? Sorry to bother you, can you update your answer with a small JS code? – Scorpio May 17 '17 at 17:31
  • Thanks Federico ..Not sure if there is any better approach but this solution work for now. – Scorpio May 17 '17 at 20:26