0

I am using ASP.NET MVC3 and I want to have an uploader feature on my project.

I followed this link and yeah, it really worked: File Upload ASP.NET MVC 3.0

But I do want a different approach.

I need to call the uploader from a Jquery and in the jQuery it will call a Controller that will return a result of true or false:

HTML code:

<div id="dialogUpload" title="Upload file"  style="display:none;">
{
   <input type="file" name="postedFile" class="button"/>
}
</div>

Jquery code:

$("#dialogUpload").dialog({
        maxheight: 400,
        maxwidth: 400,
        resizable: true,
        draggable: false,
        resizable: false,
        modal: true,
        buttons: [{
            text: "Upload",
            click: function () {
                $.ajax({
                    type: "POST",
                    url: "Controller/UploadFile",
                    dataType: "json",
                    success: function (result) {
                            if (result == true) {
                                $("#dialogUpload").dialog("close");
                                ShowAlertMessage("File successfully Uploaded.");
                            }
                            else {
                                $("#dialogUpload").dialog("close");
                                ShowAlertMessage("Failed to upload the file.");
                            }
                    }
                });

            }
        }]

    });

Inside my Controller will be this:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase postedFile)
{
    bool uploaded = false;
    if (postedFile != null && postedFile.ContentLength > 0)
    {
       var fileName = Path.GetFileName(postedFile.FileName);
       var path = Path.Combine("MYPATH",fileName);
       postedFile.SaveAs(path);
       uploaded = true;
    }
   return Content(uploaded);
}

I tried this but it doesn't return to my Jquery so I can't print a message box if it's a success or not. Please help.

Thanks.

Community
  • 1
  • 1
SyntaxError
  • 3,717
  • 6
  • 30
  • 31
  • You are Expecting dataType: "json" from server. It's not look to me that it will return json back to the views for the response of $.ajax() – Anirudha Gupta Apr 19 '13 at 13:30
  • You need to return the json if you request the json through $.ajax otherwise the response you will got (non-json) will be invalid JSON code. a common way to return json is use return json(new dictionary(){}); – Anirudha Gupta Apr 19 '13 at 13:34

2 Answers2

0

Try adding a handler to the failure event of the jQuery AJAX call. That will give you more info about why the success handler isn't being called.

In particular, make sure that the request is going to the right URL .... is your controller called ControllerController, as that's what it looks like is being expected by your AJAX call.

You could use your browser's developer tools to see the status and content returned by the AJAX call (by pressing F12 in either IE or Chrome (not sure about Firefox)).

Richard Fawcett
  • 2,799
  • 1
  • 29
  • 36
0

It should be jsonResult not ActionResult and from the controller side you have to return serialize object of your response .Then you have to just check the status of your response whether it is a success or failure if it is success then print your message according to that and if it is a failure then failure message.

like this you have to pass the json object

return Json(mew {message=true}, JsonRequestBehavior.AllowGet);
Arpit Shah
  • 37
  • 1
  • 9