How To download File In Restsharp | Download File Using Restsharp


I've just started learning RestSharp, but I'm struggling to figure out how to download a file after logging in. I have a Blob storage URL that returns a JSON response when opened in a browser. I've saved this response in a JSON file, which is 4 MB in size. 

Now, my problem is that I need to save this response in a JSON file on the server's drive. From there, I need to insert this data into a database. 

I want to make this request using code, either using the HttpClient or RestSharp library in C#. I'm unsure about what steps I should take for the above case. Can anybody suggest something? 
Answer:

Using RestSharp to Download a File

RestSharp is a popular library for making HTTP requests in C# and Here, we'll show you two of the best ways to download a file using RestSharp.

1. Downloading File Directly to Disk

We can download a file directly to disk using RestSharp's DownloadData method. This approach is suitable when we want to save the file directly to disk without loading it into memory.


    using RestSharp;

    public void DownloadFileToDisk(string fileUrl, string destinationFilePath)
    {
        // Create a RestClient instance
        var client = new RestClient(fileUrl);

        // Create a RestRequest instance
        var request = new RestRequest(Method.GET);

        // Execute the request and save the response to disk
        client.DownloadData(request).SaveAs(destinationFilePath);
    }
    

2. Downloading File to Memory Stream

If we want to download a file to a memory stream instead of saving it directly to disk, we can use RestSharp's DownloadData method with a MemoryStream. This approach is useful when we need to further process the file in memory.


    using RestSharp;
    using System.IO;

    public MemoryStream DownloadFileToMemory(string fileUrl)
    {
        // Create a RestClient instance
        var client = new RestClient(fileUrl);

        // Create a RestRequest instance
        var request = new RestRequest(Method.GET);

        // Execute the request and save the response to a MemoryStream
        byte[] fileBytes = client.DownloadData(request);
        return new MemoryStream(fileBytes);
    }
    


2

Downloading CSV & Excel Files with RestSharp

When it comes to downloading CSV and Excel files using RestSharp, we can follow a similar approach as we did for downloading a  file.

1. Downloading CSV File

To download a CSV file, we'll use RestSharp's DownloadData method to retrieve the file content as a byte array. Then, we'll save the byte array to a file with the .csv extension.


    using RestSharp;
    using System.IO;

    public class CsvDownloader
    {
        public void DownloadCsv(string csvUrl, string destinationFilePath)
        {
            // Create a RestClient instance
            var client = new RestClient(csvUrl);

            // Create a RestRequest instance with GET method
            var request = new RestRequest(Method.GET);

            // Download the CSV file as a byte array
            byte[] csvBytes = client.DownloadData(request);

            // Save the byte array to a file with .csv extension
            File.WriteAllBytes(destinationFilePath, csvBytes);
        }
    }
    

2. Downloading Excel File

Similarly, to download an Excel file, we'll use RestSharp's DownloadData method to retrieve the file content as a byte array. Then, we'll save the byte array to a file with the .xlsx extension.


    using RestSharp;
    using System.IO;

    public class ExcelDownloader
    {
        public void DownloadExcel(string excelUrl, string destinationFilePath)
        {
            // Create a RestClient instance
            var client = new RestClient(excelUrl);

            // Create a RestRequest instance with GET method
            var request = new RestRequest(Method.GET);

            // Download the Excel file as a byte array
            byte[] excelBytes = client.DownloadData(request);

            // Save the byte array to a file with .xlsx extension
            File.WriteAllBytes(destinationFilePath, excelBytes);
        }
    }
    
3

Downloading a PDF with RestSharp

When we need to download a PDF file using RestSharp, we follow a straightforward process. Here's how we can do it:

1. Create a RestClient Instance

We start by creating an instance of the RestClient class, specifying the URL of the PDF file we want to download.


    using RestSharp;

    var client = new RestClient("pdf-url-here");
    

2. Create a RestRequest Instance

Next, we create a RestRequest instance with the HTTP method set to GET, as we are fetching the PDF file from the server.


    var request = new RestRequest(Method.GET);
    

3. Download the PDF File

Now, we use RestSharp's DownloadData method to download the PDF file content as a byte array.


    byte[] pdfBytes = client.DownloadData(request);
    

4. Save the PDF File

Finally, we save the byte array to a file with the .pdf extension, completing the download process.


    System.IO.File.WriteAllBytes("destination-path.pdf", pdfBytes);
    

With these steps, we can easily download a PDF file using RestSharp in our C# application.

4

Using RestSharp to Download a File (Async Version)

RestSharp also supports asynchronous operations, allowing us to download files asynchronously. Here are the async versions of the previous methods:

1. Downloading File Directly to Disk (Async)

We can download a file directly to disk asynchronously using RestSharp's DownloadDataAsync method.


    using RestSharp;
    using System.Threading.Tasks;

    public async Task DownloadFileToDiskAsync(string fileUrl, string destinationFilePath)
    {
        // Create a RestClient instance
        var client = new RestClient(fileUrl);

        // Create a RestRequest instance
        var request = new RestRequest(Method.GET);

        // Execute the request asynchronously and save the response to disk
        byte[] fileBytes = await client.DownloadDataTaskAsync(request);
        await Task.Run(() => File.WriteAllBytes(destinationFilePath, fileBytes));
    }
    

2. Downloading File to Memory Stream (Async)

We can also download a file to a memory stream asynchronously using RestSharp's DownloadDataAsync method.

 using RestSharp;
    using System.IO;
    using System.Threading.Tasks;

    public async Task<MemoryStream> DownloadFileToMemoryAsync(string fileUrl)
    {
        // Create a RestClient instance
        var client = new RestClient(fileUrl);

        // Create a RestRequest instance
        var request = new RestRequest(Method.GET);

        // Execute the request asynchronously and save the response to a MemoryStream
        byte[] fileBytes = await client.DownloadDataTaskAsync(request);
        return new MemoryStream(fileBytes);
    }