Search code examples
c#asp.net-mvc-3uploadify

How do get a controller Action to execute after uploadify?


I don't understand the details of what is happening behind the scenes with MVC3 and Uploadify. I need to allow the page below to upload multiple files. That works so far. I can upload files, they get saved to the server. The problem is I need to have the page reload with a summary of the list of the uploaded files. This would be like a postback in asp.net webforms.

@model Tangible.Models.UploadViewModel

        <input id="uploadfile" name="uploadfile" type="file" class="uploadify"  />

The template provided outlines the upload headings and format for Disposed Assets, Leased, Loaned, Rented, and Business Assets. 

The files must be uploaded in Microsoft Excel format following the templates provided. Each template must be a separate worksheet within the Excel file being uploaded. Prior to uploading, please review the template in Excel format which requires all headings be located in row one. Do not use commas in numeric fields but the decimal place character “.” is required when indicating cents. If you have any questions or concerns regarding the format please contact our Tangible Personal Property Department at 941.861.8291.

Note: Summary schedule item 21.- Pollution Control Equipment must be accompanied by the current DEP certificate for the reported equipment. (Please include an upload for a DEP Certificate)



        <script type="text/javascript">
            $(document).ready(
            function () {
            @{ var auth = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;}
                var auth = "@auth";
                var ASPSESSID = "@Session.SessionID";                

                $(".uploadify").uploadify({
                    'uploader': '@Url.Content("~/Scripts/uploadify.swf")',
                    'cancelImg': '@Url.Content("~/Images/cancel.png")',
                    'buttonText': 'Upload',
                    'script': '@Url.Content("~/Wizard/Upload")',
                    'fileDesc': 'Files',
                    'fileExt': '*.*',
                    'auto': true,
                    'scriptData': { ASPSESSID: ASPSESSID, AUTHID: auth}
                });
            }
        );

            </script>

-- Controller Action: Needs to somehow do a final step after all files are uploaded to postback

  [HttpPost]
        public ActionResult Upload(HttpPostedFileBase FileData)
        {
            var saveLocation = Path.Combine(Server.MapPath("\\"), "returns\\" + DR405Profile.CurrentUser.TangiblePropertyId);
            System.IO.Directory.CreateDirectory(saveLocation);
            FileData.SaveAs(Path.Combine(saveLocation, FileData.FileName));
            ViewData["UploadStatus"] = String.Format("File name: {0}, {1}Kb Uploaded Successfully.", FileData.FileName, (int)FileData.ContentLength / 1024);
            return View();
        }

Solution

  • You could subscribe to the onAllComplete event on the client and do whatever you want to do there. For example you could reload the current url, send an AJAX request to refresh only a portion of the url, ... Explore the documentation, you might find interesting options and events to subscribe to.