Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - JQuery Ajax call for PDF file download

My question is similar to Download and open pdf file using Ajax

But not exactly the same , the reason I want an JQuery ajax is that my file is being generated dynamically from the data which would be fetched from the same page.

So basically I have a Json Object which needs to be sent to the server which would dynamically generate a file and send it back to the browser.

I would not like to use query strings on an anchor with my Json object stringyfied , as I think it would be a potential threat as query strings have character restrictions ( am I right here ?).

Please let me know If my workflow is right or I can achieve the same thing thing using a different flow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not use AJAX to download files. This doesn't work. This being said you are left with 2 options:

  1. action link and query string parameters or if the download must be trigerred at some specific javascript event you could also set the window.location.href to the action that is supposed to generate the PDF file and pass query string parameters to it.

  2. or if you are afraid that you have lots of data to pass to the controller action that will generate the PDF to download you use a <form> with method="POST" and inside you use hidden fields to store as much parameters and data as you like:

    @using (Html.BeginForm("download", "home"))
    {
        ... hidden fields containing values that need to be sent to the server
        <input type="submit" value="Download PDF" />
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...