MVCRazorToPDF保存为PDF位置或斑点,然后下载 [英] MVCRazorToPDF save PDF to location or Blob and then download

查看:151
本文介绍了MVCRazorToPDF保存为PDF位置或斑点,然后下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

 public class ActionDownloadAttribute : ActionFilterAttribute 
 {
       public override void OnResultExecuted(ResultExecutedContext filterContext)
       {
             filterContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "Report.pdf");
             base.OnResultExecuted(filterContext);
       }
 }


[ActionDownload]
 public ActionResult GeneratePdf()
 {
       List<Comment> comments = null;
        using (var db = new CandidateEntities())
        {
            comments = db.Comments.ToList();
        }
        return new PdfActionResult("GeneratePdf", comments);
 }

这上面code给出了download.But PDF文件我想将它保存(自动),以特定的路径或BLOB下载前。

This above code gives the PDF file for download.But I want to Save it(Automatically) to specific path or Blob before downloading it.

谁能帮助我一样吗?

推荐答案

当我本来看着我的回答并没有真正带来了很多value.So的我会尽量扩大。

When I looked originally at my answer it did not really brought a lot of value.So I will try to expand.

首先,我有完全一样的控制器你。然后我用restsharp拨打电话到该网址

First I have exactly the same controller as you. Then I use restsharp to make a call to that URL

        var client = new RestClient("http://some.url.com");

        var request = new RestRequest("mvc/GeneratePDF", Method.GET);
        // execute the request
        RestResponse response = (RestResponse)client.Execute(request);

        // Zwracamy byte[] ktory jest naszym plikiem PDF
        return response;

现在,如果你看一下 response.RawBytes 您可以使用下面的方法来将字节数组直接上传到Azure的:)

Now if you look at the response.RawBytes you can use that in methods below to upload the byte array directly to Azure :)

我把我的2种方法之一,无论是上传一个byte []或流

I call one of my 2 methods to either upload a byte[] or from stream

 public static class AzureStorage
{
    /// <summary>
    /// Metoda zajmujaca sie uploadem do Azure
    /// </summary>
    public static string _uploadToAzureBlob(byte[] arrPDF, string azureContainer, string filename)
    {
        // Retrieve connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConn"));
        // Create blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(azureContainer);
        // Create object blob 
        CloudBlockBlob blob = container.GetBlockBlobReference(filename);
        // Upload
        blob.UploadFromByteArray(arrPDF, 0, arrPDF.Length);

        return blob.Uri.ToString();
    }

    /// <summary>
    /// Metoda zajmujaca sie uploadem do Azure
    /// </summary>
    public static string _uploadToAzureBlob(Stream iostream, string azureContainer, string filename, bool ApplyReadPermissions = true)
    {
        // Retrieve connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConn"));
        // Create blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(azureContainer);

        // Create container if it does not exist
        container.CreateIfNotExists();

        // Create object blob 
        CloudBlockBlob blob = container.GetBlockBlobReference(filename);

        iostream.Position = 0;//Move the pointer to the start of stream..

        using (var fileStream = iostream)
        {
            blob.UploadFromStream(fileStream);
        }

        // Here we need to share the URL for reading the barcode! Otherwise we dont have access to it
        if (ApplyReadPermissions)
        {
            var builder = new UriBuilder(blob.Uri);
            builder.Query = blob.GetSharedAccessSignature(
                new SharedAccessBlobPolicy
                {
                    Permissions = SharedAccessBlobPermissions.Read,
                    SharedAccessStartTime = new DateTimeOffset(DateTime.UtcNow.AddMinutes(-5)),
                    SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddMinutes(5))
                }).TrimStart('?');

            var x = builder.Uri.ToString();

            return x;
        }

        return null;
    }
}

让我知道,如果这有助于。这对我的作品在我的蔚蓝的环境。我有一个webjob生成这些文件和BLOB自动保存它们。

Let me know if this helps. It works for me in my azure environment. I have a webjob generating those files and saving them automatically on blob.

如果你重写这些方法上传到Azure的是异步然后有像下面的东西将使你做下面的调用:

If you would rewrite those methods to upload to Azure to be Async then having something like below would enable you to do following call :

    public async Task<ActionResult> asyncPDF()
    {
        return await AzureStorage._uploadToAzureBlob( ControllerContext.GeneratePdf(objModel, "VIEW_NAME") ) ;
    }

我将测试这种方法更详细地确认其行为。

I will be testing that method more in detail to confirm its behavior.

意见多的欢迎:D

这篇关于MVCRazorToPDF保存为PDF位置或斑点,然后下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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