I need to program an API that has 3 parts:
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.
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.