Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a quick example I made up. This is the concept LukLed was talking about with calling SaveFile but don't return file contents via ajax and instead redirect to the download.</p> <p>Here's the view code:</p> <pre><code>&lt;script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function() { // hide form code here // upload to server $('#btnUpload').click(function() { $.ajax({ type: 'POST', dataType: 'json', url: '&lt;%= Url.Action("SaveFile", "Home") %&gt;', success: function(fileId) { window.location = '&lt;%= Url.Action("DownloadFile", "Home") %&gt;?fileId=' + fileId; }, error: function() { alert('An error occurred uploading data.'); } }); }); }); &lt;/script&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;div&gt;Field 1: &lt;%= Html.TextBox("field1") %&gt;&lt;/div&gt; &lt;div&gt;Field 2: &lt;%= Html.TextBox("field2") %&gt;&lt;/div&gt; &lt;div&gt;Field 3: &lt;%= Html.TextBox("field3") %&gt;&lt;/div&gt; &lt;button id="btnUpload" type="button"&gt;Upload&lt;/button&gt; &lt;% } %&gt; </code></pre> <p>Here's the controller code:</p> <pre><code>[HandleError] public class HomeController : Controller { public ActionResult Index() { return View(); } public JsonResult SaveFile(string field1, string field2, string field3) { // save the data to the database or where ever int savedFileId = 1; // return the saved file id to the browser return Json(savedFileId); } public FileContentResult DownloadFile(int fileId) { // load file content from db or file system string fileContents = "field1,field2,field3"; // convert to byte array // use a different encoding if needed var encoding = new System.Text.ASCIIEncoding(); byte[] returnContent = encoding.GetBytes(fileContents); return File(returnContent, "application/CSV", "test.csv"); } public ActionResult About() { return View(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload