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

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

问题描述

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

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

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

这是测试的html代码:

<脚本 >$('#btnSendRequest').on('click', function() {$.ajax({类型:POST",url: "/Convertor/Html",数据: {strUrl: "http://make-sense.co.il/kb/avcp-script-installation.pdf"},成功:功能(数据){返回真;},});});</script>

<html xmlns="http://www.w3.org/1999/xhtml"><头><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script><title>测试员</title><身体><h1>测试人员html</h1><div><input id="btnSendRequest" type="button" value="SendHttpRequest"/>

My ActionResult function: "convertor/html" ,从网页中获取 url 字符串.我需要的是当我点击按钮时,pdf文件会自动下载到我的服务器.

public ActionResult Html(string strUrl){返回视图();}

有谁知道如何做到这一点?我还在某处阅读了一些名为 base64 编码的内容,这可能也是解决方案,但我以前从未使用过它.

提前致谢.

解决方案

你可能要找的是 .NET 上的 WebClient,看下面的例子,我只是从网上的一个例子中抓取的,参见 此处 全文.

使用系统;使用 System.Net;使用 System.IO;课程计划{静态无效主(){使用 (WebClient 客户端 = new WebClient()){//下载数据.byte[] arr = client.DownloadData("http://url-to-your-pdf-file.com/file1");File.WriteAllBytes(path_to_your_app_data_folder, arr)}}}

您需要通过将 byte[] 保存为某个文件来进行进一步处理.上面的示例代码适用于控制台应用程序,但同样可以在您的 mvc 控制器中实现.

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.

解决方案

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.

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

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