How do I download a file using Curl? [JavaScript/AJAX Code]

To download a file with Curl, use the --output or -o command-line option. This option allows you to save the downloaded file to a local drive under the specified name. If you want the uploaded file to be saved under the same name as in the URL, use the --remote-name or -O command line option. If you don't use any of these options, Curl will print the downloaded file content to the screen. In this Curl Download File example, we download a file from the ReqBin echo URL. For security reasons, saving files on the ReqBin server is disabled. The JavaScript/AJAX code was automatically generated for the Curl Download File example.
How do I download a file using Curl? [JavaScript/AJAX Code] Run
curl -o filename.txt
Updated: Viewed: 26817 times
JavaScript/AJAX code for Curl Download File example

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 a different directory, change the working directory before running the Curl command. 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 Syntax
curl -o [file name] [URL]

Curl Download File Example

The following is an example of downloading a file using Curl:

Curl Download File
curl -o page.html

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:

Use Remote File Name
curl -O [URL]

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:

Curl Download Multiple Files
curl [URL-1] [URL-2] [URL-3]
   -o [file-name-1]
   -o [file-name-2]
   -o [file-name-3]

How can I see the download progress?

By default, Curl shows no progress when downloading a file. To see the download progress, use the "-#" flag. The following is an example of printing download progress using the Curl flag:

Curl Shoe Download Progress
curl -# -O [URL]

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:

Curl Silent Download Mode
curl -s -O [URL]

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.

Curl Limit Download Speed
curl --limit-rate 100k -O [URL]

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your Curl Download File request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.