Search code examples
javascriptasp.net-mvc-3file-uploadjsonresult

How to show an alert after the file was successfully uploaded in ASP.net MVC 3


For now I am able to upload the file successfully... What I'm trying to do right now is show an alert box if the file was successfully uploaded or show an alert for the error/exception if not...

Here is my view:

using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { @id = "File", enctype = "multipart/form-data"}))
        {
        <div class="control-group">                
            <input type="file" id="file" name="file" />
            <input type="submit" value="Upload" />
        </div>
        }

Here is my controller:

[HttpPost]   
public ActionResult Upload(HttpPostedFileBase file)
{
    try
    {
        //some code here for the upload....             

        /*In this part is my problem...Both Lines below is not showing the alert...*/
        //return new JavaScriptResult() { Script = "alert('The calendar XML file was uploaded successfully!');" };
        //return JavaScript("alert('The calendar XML file was uploaded successfully!');");
    }

    catch (Exception e)
    {   
        log.Error("HomeController::Upload()", e);
        return new JavaScriptResult() { Script = "alert(\"" + e.Message + "\");" };
    }
}

What i would like is that my view would still remain... no redirection of page... just merely showing of that alert box for the message...Thank you! Any ideas is greatly appreciated coz i know this way of mine is not recommended... :)


Solution

  • I was able to find the right solution here.

    The link above was for showing a confirmation box before submitting the form and showing an alert for the message that was a json object returned by my controller...

    return Json(new { isok = true, message = "Successfully uploaded the calendar XML file!" }, "application/octet-stream");