1

I need to program an api that has 3 parts:

  1. Get the pdf file from pdf url.
  2. Converting the pdf.
  3. Return the converted pdf file.

I have already completed part 2 and 3, What left is to fetch the pdf from url and copy/download it to my mvc web api.

This is the test html code:

< script >
  $('#btnSendRequest').on('click', function() {
    $.ajax({
      type: "POST",
      url: "/Convertor/Html",
      data: {
        strUrl: "http://make-sense.co.il/kb/avcp-script-installation.pdf"
      },
      success: function(data) {
        return true;
      },
    });
  }); < /script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <title>tester</title>
</head>

<body>
  <h1>tester html</h1>
  <div>
    <input id="btnSendRequest" type="button" value="SendHttpRequest" />

  </div>

My ActionResult function: "convertor/html" , gets the url string from the web page. What I need is when I click the button, the pdf file will be automatically download to my server.

public ActionResult Html(string strUrl) 
    {
        return View();
    }

Anyone has any idea how that can be done? I also read somewhere on something called base64 encoding that might be also the solution, but I have never used it before.

Thanks in advance.

barak
  • 147
  • 1
  • 3
  • 17

2 Answers2

5

What you might be looking for is the WebClient on .NET, see the following example, I just grabbed it from an example online, see here for full article.

using System;
using System.Net;
using System.IO;

class Program
{
    static void Main()
    {
    using (WebClient client = new WebClient())
    {

        // Download data.
        byte[] arr = client.DownloadData("http://url-to-your-pdf-file.com/file1");

        File.WriteAllBytes(path_to_your_app_data_folder, arr)

    }
    }
}

you will need to do further processing by saving the byte[] as a file somewhere. the example code above is for a console app, but the same can be implemented in your mvc controller.

Ahsan
  • 2,488
  • 2
  • 22
  • 44
  • But please notice your code snippet is a desktop app, while the question is about mvc web app –  Jun 26 '16 at 09:43
  • I succsseded to get the base64 of this pdf file by using stream. Now I need to convert this base64 string back to pdf but I am getting an error message which I'm trying to figure – barak Jun 26 '16 at 09:46
  • Converting to base64 is unnecessary, please consider working with binary data with the WebClient to download the file to your server. – Ahsan Jun 26 '16 at 09:56
  • Edit: I got StreamReader, which is not base64. I am really stuck here. – barak Jun 26 '16 at 10:12
  • I've updated the answer to show you how to save the file somewhere in the file system. – Ahsan Jun 26 '16 at 10:17
  • Glad to be of help, please mark it as the answer if you are happy with it. – Ahsan Jun 26 '16 at 11:12
-1
            var request = System.Net.WebRequest.Create("http://make-sense.co.il/kb/avcp-script-installation.pdf");
            request.Method = "GET";

            using (var response = request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var fileStream = System.IO.File.Create(@"path/to/file"))
                    {
                        stream.CopyTo(fileStream);
                    }
                }
            }