3

I'm wondering how I can return a javascript alert when the file that usually gets generated is not created in the folder. When the else statement is ran, it returns the literal text at the top of the browser tab instead of the alert that I am looking for. It looks like this:

Browser text

Code:

public ActionResult DownloadFile(string path, string fileName)
{
    if (System.IO.File.Exists(path))
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        return File(fileBytes, "application/force-download", fileName);
    }
    else 
    {
        return Content("<script language='javascript' type='text/javascript'>alert('No data was found to create a CSV file!');</script>");

    }
}
4
  • 1
    I'm not sure you should Commented Jun 8, 2020 at 18:23
  • Content() sets the http content type header to "text/plain". That's why no html is rendered. See learn.microsoft.com/en-us/dotnet/api/… to return html with correct status code Commented Jun 8, 2020 at 18:50
  • @ChristophLütjen I've added a contentType parameter with "text/javascript", and that does not seem to change the output. Commented Jun 8, 2020 at 18:58
  • Given your example, you're returning html (text/html) not javascript. Commented Jun 9, 2020 at 9:13

1 Answer 1

2

Firstly you can use the method public virtual ContentResult Content(string content, string contentType); rather than public virtual ContentResult Content(string content);

Controller:

public ActionResult DownloadFile()
        {
            return Content("alert('No data was found to create a CSV file!');", "application/javascript");
        }

In addition,you can also write a result which has a Parametrical constructor and extends ContentResult.You can refer to it

Here is a demo worked: Controller:

public ActionResult DownloadFile()
    {
        //return Content("alert('No data was found to create a CSV file!');", "application/javascript");
        return new JavaScriptResult("alert('No data was found to create a CSV file!');");
    }
    public ActionResult DownloadFile1() {
        return View();
    }

    public class JavaScriptResult : ContentResult
    {
        public JavaScriptResult(string script)
        {
            this.Content = script;
            this.ContentType = "application/javascript";
        }
    }

DownloadFile1:

@{
    ViewData["Title"] = "DownLoadFile1";
}

<h1>DownLoadFile1</h1>

<div>
    <partial name="DownLoadFile" />
</div>
@section scripts{
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script>
        $(function () {
            $.getScript("/Test/DownloadFile");
        });
    </script>
}

Result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.