Search code examples
jquerydebuggingasp.net-mvc-3uploadify

Can not return ActionResult from method when using Uploadify MVC3 application


If I use this as my Controller Action...FAIL.

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

When I run my web app and try to upload a file I get a progress bar that works, gets to a random percentage, then returns a generic error.

enter image description here

However!!

This Controller Action works perfect.

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

Here is the code from my upload page. All the script references are loaded in site.master.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>



<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewBag.Message %></h2>
    <%= this.Profile.TaxPayerID  %>
    <p>Please upload your return</p>   
<script type="text/javascript">
    $(document).ready(
    function () {
        $("#fileuploader").fileUpload({
            'uploader': '/Scripts/uploader.swf',
            'cancelImg': '/Images/cancel.png',
            'buttonText': 'Select DR405',
            'script': 'Home/Upload',
            'folder': '/returns',
            'fileDesc': 'Image Files',
            'fileExt': '*.csv;*.txt;*.xls;*.xlsx',
            'auto': true
        });
    }
);
    </script>

<div id="fileuploader"></div>
</asp:Content>

Why can't I return ActionResult?


Solution

  • Noticing a few things here. Not sure why your MVC3 app has asp tags and a page declaration header. The uploadify requires an input tage type="file" with a unique ID on the page, but your example points to a div tag. Try changing that to a <input type="file"/> tag.