Ajax return file for download

Ajax return file for download

ajax return file for download

javascript: var xhr = new XMLHttpRequest(); www.cronistalascolonias.com.ar("GET", "www.cronistalascolonias.com.ar");. Now the task is to download this response(www.cronistalascolonias.com.ar file) on User's local system without user being redirected to an URL or a prompt asking him/her to click a button. With a bit of extra work, we can download this file to our file system: const jpegData = www.cronistalascolonias.com.arse; const blob = new Blob([jpegData], { type });. ajax return file for download

Ajax return file for download - opinion

Programmer Group

I have a struts 2 operation on the server side for file download.

However, when I call an operation using jQuery:

In Firebug, I see that the data is retrieved through the Binary stream. I want to know how to open the file download window so that users can save files locally?

#1 building

1. Framework independent: Servlet download file as attachment

2. Struts 2 Framework: download files as attachments

It is best to use < s: a > tags with OGNL to point to URLs created with < s: URL > Tags:

In this case, you need to write the content disposition header to the response to specify that you need to download the file (attachment) and that the file (inline) cannot be opened by the browser. You also need to specify the content type, and you may need to add a filename and length (to help the browser draw a realistic progress bar).

For example, when downloading a ZIP:

Use struts 2 (unless you use Action as a Servlet, for example, Direct flow Hackers program ), you don't need to write anything directly to the response; just use the Stream result type And configure in www.cronistalascolonias.com.ar: Example

3. Agnostic framework (/ Struts2 framework): Servlet (/ Action) opens files in browser

If you want to open a file in the browser instead of downloading a file, you must set the content disposition to inline, but the target cannot be the current window location; you must locate a new window created using javascript, < iframe > in the page, or a new window created dynamically using "discussed target" = "U blank":

#2 building

I've created some features as a solution (inspired by the @ John culviner plug-in):

Demo with click event:

#3 building

OK, based on the ndpu code, this is an improved version of Ajax download (I think)-

Use in this way-

These parameters are sent as appropriate post parameters, just as they are from the input, rather than as json encoded strings in the previous example.

CAVEAT: beware of the possibility of variable injections on these forms. There may be a safer way to code these variables. Or consider escaping it.

#4 building

OK, this is the working code when using MVC. You are getting the file from the controller

It can be said that you declare and fill the byte array, and the only thing to do is to use the File function (using www.cronistalascolonias.com.ar)

Then add those two functions to the same controller

You can then call the controller to download and get a success or failure callback

#5 building

You can use HTML5

Note: the returned file data must be base64 encoded because you cannot JSON encode binary data

In my AJAX response, I have a data structure as follows:

This means that I can do the following to save the file via AJAX

The base64ToBlob function is taken from here , must be used with this function

This is good if your server is dumping file data to save. However, I haven't figured out how to implement HTML4 backup advertising

Posted by Greaser on Mon, 10 Feb

Источник: www.cronistalascolonias.com.ar

Ajax return file for download

1 thoughts to “Ajax return file for download”

Leave a Reply

Your email address will not be published. Required fields are marked *