
JavaScript/AJAX code for Curl Download File Example
This JavaScript/AJAX code snippet was generated automatically for the Curl Download File example.<< Back to the Curl Download File example
What is Curl?
Curl is an open source command line utility and cross-platform libcurl library for sending HTTP requests from clients to servers. Curl supports all popular Internet protocols including HTTP and HTTPS. Curl has built-in support for validating certificates, SSL, HTTP Cookies and is used to debug network requests and API calls. Curl works on all modern platforms including Windows, macOS and Linux.
How to download files using Curl?
By default, when you request a resource from the server using Curl, it downloads the resource and displays its contents on the screen. When using Curl, you must explicitly request that the downloaded resource be saved to disk by using the --output (-o) or --remote-name (-O) command-line options. The file will be saved to the current directory that you are working in. To save the file in another directory, put the directory before the file name, for example -o downloads/page.html. In Curl 7.73.0 and later, you can also set the directory with --output-dir [directory]; it works with both -o and -O. If the directory does not exist, add --create-dirs and Curl will create it. If the file already exists, it will be overwritten.
If you want to give the downloaded file a specific name, you must use the --output or -o option with the following syntax:
Curl Download File Example
Curl downloads any type of file the same way, such as an image, a PDF or a ZIP archive. The following is an example of downloading a file using Curl:
How to save Curl output to a file?
To save a server response to a file, add -o and a file name to the Curl command. Curl writes the response to the specified file instead of printing its contents on the screen. To append the response to the end of an existing file, put >> [file name] at the end of the command.
How to save the file with its original name?
To save the downloaded file with the same name as specified in the URL, use the --remote-name (or -O) command line parameter. Curl will only take file part and ignore the path. Below is an example of downloading a file using Curl, keeping its original name:
How to download multiple files using Curl?
To download multiple files using Curl, you can specify multiple URLs and filenames in one Curl command. Curl will save the first URL under the first file name, the second URL under the second file name, etc. Below is an example of downloading multiple files using Curl:
How can I see the download progress?
When you save a file with -o or -O, Curl shows the download progress by default: the size received so far, the speed and the time left. To see a download progress bar, use the -# (--progress-bar) flag. The following is an example of a download with a progress bar:
How to enable silent download mode in Curl?
If you don't want to see any information while downloading the file, you can turn on silent mode to prevent Curl from printing anything. The silent mode can be enabled with the --silent (-s) command-line parameter. This can be useful when using Curl in automation scripts. Below is an example of silent downloading of a file in Curl:
How to limit download speed in Curl?
Curl allows you to limit the maximum download speed using the --limit-rate flag. This can be useful when using Curl on slow Internet connections. Download speed is measured in bytes per second. If you want to specify the speed in kilobytes, add the suffix "k" or "K", or "m" or "M" for megabytes. The suffixes (k, M, G) are 1024 in size. In this Curl Download File example, we limit the download speed to 100 kilobytes per second.