MVC-如何从网址请求和保存pdf文件 [英] MVC - How to request and save a pdf file from a url

查看:58
本文介绍了MVC-如何从网址请求和保存pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个包含3个部分的api:

I need to program an api that has 3 parts:

  1. 从pdf网址获取pdf文件.
  2. 转换pdf.
  3. 返回转换后的pdf文件.

我已经完成了第2部分和第3部分, 剩下的就是从网址中获取pdf并将其复制/下载到我的mvc网络api中.

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.

这是测试html代码:

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>

我的ActionResult函数:"convertor/html",从网页获取url字符串. 我需要的是,当我单击按钮时,pdf文件将自动下载到我的服务器上.

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();
    }

任何人都知道该怎么做? 我还读过有关base64编码的内容,这也许也是解决方案,但我以前从未使用过.

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.

谢谢.

推荐答案

您可能正在寻找的是.NET上的WebClient,请参见以下示例,我只是从示例中在线获取的,请参见

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)

    }
    }
}

您将需要通过将byte []另存为文件进行进一步处理.上面的示例代码是针对控制台应用程序的,但是可以在mvc控制器中实现相同的代码.

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.

这篇关于MVC-如何从网址请求和保存pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆