0

Please read the question before flagging as duplicate. I'm trying to download a file by redirecting with Javascript to a controller action in my ASP.NET MVC3 project. It works perfectly in Firefox and IE8. Here's my Javascript code:

<script type="text/javascript">
    $(function(){
        $(".pdf").click(function(e) {
            e.preventDefault();
            $.post("@Url.Action("PDF","Helper")", {id : @Model.ID} ,function(data){
                if(data.message == "File not found.") {
                    alert(data.message);
                } else {
                    location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })';
                }
            });
        });
    });
</script>

Here's what I tried:

  1. I added return false; with all the things below

  2. I changed the line to:

    window.location.href = "../../Helper/PDF/@Model.ID";
    
  3. I tried window.location instead

  4. I changed relative link to full link

  5. Tried without window

  6. Tried:

    setTimeout(function() {
        document.location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })'
    },500);
    
  7. I added the line:

    window.event.returnValue = false;
    

    it gave the error "window.event is not defined". I replaced it with e.

  8. I wrote "http://www.google.com" instead, and it didn't work too. However, it worked in a separate html file only consists of location.href = "http://www.google.com";.

All of this worked in IE8 and Firefox but not in Chrome. What can I do to resolve this?

EDIT: This is generated script:

$(function(){
    $(".pdf").click(function(e) {
        e.preventDefault();
        $.post("/Helper/PDF", {id : 130405002} ,function(data){
            if(data.message == "File not found.") {
                alert(data.message);
            } else {
                location.href = '/Helper/PDF/130405002';
            }
        });
    });
});

This is the generated HTML where I call the function:

<input class="pdf" type="button" value="Download File" />

Debugging the action I got these results; at the console Status is "failed". Type is "application/pdf" as expected and Size is 198 kb, it's the file size. Method is POST.

This is my console output:

console output

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64
  • 1) what is class="pdf" ? links? Can we see them? 2) can you show the rendered html and rendered script? 3) hit F12 and tell us what the console has to say 4) why not – mplungjan May 08 '13 at 12:24
  • Can we also have the HTML for the button etc... – Menelaos May 08 '13 at 12:29
  • what `console.log` says? – Ruslan Polutsygan May 08 '13 at 12:31
  • @mplungjan I return file if it's found, else i return a fail message as JSON object. I can do other workarounds I know, but I want to know why this doesn't work. – İsmet Alkan May 08 '13 at 12:58
  • There is no need to return false on the click of a type=button. Now time to look in the console NET tab to see why it is failed – mplungjan May 08 '13 at 12:59
  • @mplungjan i already answered your questions with my edit – İsmet Alkan May 08 '13 at 13:01
  • Yes, I saw that some were answered after I posted the comment. Can you not use a form? – mplungjan May 08 '13 at 13:02
  • if I use a form, I can get the file result but cannot just alert the error message. above all, i just want the answer to this question, i already said i can do other workarounds. – İsmet Alkan May 08 '13 at 13:04
  • Okay, so , is there a post size limit applied on your server. Normally the safe size in such cases is 2KB. In your case the pdf size is 198 kb. Probably , you can check your server settings to see the post size and try to play with it. Here's the link with something similar : http://stackoverflow.com/questions/2364840/what-is-the-size-limit-of-a-post-request .BTW, what is your IDE ? – The Dark Knight May 08 '13 at 13:22
  • visual studio 2010. i said it's working in Firefox and IE8, I don't think that has nothing to do with server limits. – İsmet Alkan May 08 '13 at 13:27

1 Answers1

0

I changed the script with the following:

<script type="text/javascript">
$(function () {
  $(".pdf").click(function () {
    $.get('@Url.Action("PDFAvailable", "Helper")', { id: @Model.ID }, function (data) {
      if(data.message == "File not found.") {
        alert(data.message);   
      } else {
        location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })';
      }
    }); 
  });
});
</script>

You see, I added a new controller action, which only controls if the file exists and call the file action. This works across all browsers, at least the ones I have. I don't exactly know, but this might be a bug or security precaution in Chrome.

İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64