下面是我的代码:
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);
}
这上面的代码提供download.But PDF文件我想下载之前将其保存(自动),以特定的路径或斑点。
谁能帮我一样吗?
当我在我的答案原本看上去它并没有真正带来了很多value.So的我会尽量扩大。
不是异步
首先,我有完全一样的控制器你。 然后我用restsharp使该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的:)
我调用我的2种方法之一或者上传字节[]或来自流
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自动保存它们。
更新:异步
如果你想重写这些方法上传到Azure的是异步然后有像下面的东西将使你做以下电话:
public async Task<ActionResult> asyncPDF()
{
return await AzureStorage._uploadToAzureBlob( ControllerContext.GeneratePdf(objModel, "VIEW_NAME") ) ;
}
我将更加考验这个方法进行了详细确认其行为。
注释远迎:d