Doug Chamberlain
Doug Chamberlain

Reputation: 11341

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?

Upvotes: 0

Views: 793

Answers (2)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93571

The typical upload handler (ashx version) for uploadify does something like this:

//  if the file was saved successfully...
{
    context.Response.Write("1");
}
else
{
    // Let client know about failure
    context.Response.Write("0");
}

That means that the client uploader is expecting a response that is either a simple 1 or 0 text string to say if it succeeded. That is probably the simplest return they could work with.

The equivalent in MVC is to return a string "0" or "1" in response to a successful save. That means you need a simple return type of string. If you return a 'View()' as an ActionResult the client uploader gets back an entire HTML page from the server and does not know what to do with it (hence the error).

If you view the working and non-working version in Fiddler2 (highly recommended) you would have seen the difference instantly.

Upvotes: 0

Jay
Jay

Reputation: 6294

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.

Upvotes: 1

Related Questions