File downloads are simple:
You can use hyperlinks in HTML to jump to a file for downloading:
<?php echo ' <a href = "http://www.localhost/download/1.zip" > Download </a> ';? >
The use method is the user directly clicks the hyperlink in the browser.
But this download method has some problems: some browsers can directly identify the type of data, such as text, part of the image and so on. will be displayed directly in the browser rather than the download.
Therefore, this method is not usually used.
In PHP, you can use the header function to set the server's file as an attachment type to notify the customer of the download. Use the following:
Header (' Content-type:application/zip ');
Header (' content-disposition:attachment; filename= "1.zip");
ReadFile (' 1.zip ');
It should be noted that when using header downloads, clients can only use the <a> tag request to the action in the controller on the server side (different from the above to the specific file).
Can I use Ajax? The answer is no.
Ajax sending requests is certainly not a problem, but the data retrieved by Ajax is a JSON string. So the file stream cannot be implemented.
Uploading files can still be Ajax. Because the data type that Ajax sends is not limited to JSON strings.