简体   繁体   English

如何使用带有response.WriteResponseStreamToFile的下载文件夹下载文件? (Amazon S3,C#)

[英]How do I download a file using to the downloads folder with response.WriteResponseStreamToFile? (Amazon S3, C#)

I have some PDFs stored in S3 and I'm trying to make a link that will download them. 我在S3中存储了一些PDF,我正在尝试建立一个链接来下载它们。 This is the method that I have so far: 到目前为止,这是我的方法:

public virtual ActionResult DownloadPDF(string filename)
{
    string secretKey = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_SecretKey");
    string accessKey = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_AccessKey");
    var bucket = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_BucketName");
    var serviceUrl = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_ServiceUrl");

    AmazonS3Config config = new AmazonS3Config();
    config.ServiceURL = serviceUrl;

    var client = Amazon.AWSClientFactory.CreateAmazonS3Client(
            accessKey,
            secretKey,
            config
            );
    GetObjectRequest request = new GetObjectRequest();
    request.BucketName = bucket;

    request.Key = "userfiles/MSD IMAGES/ProductDocumentation/" + filename;
    GetObjectResponse response = client.GetObject(request);
    response.WriteResponseStreamToFile("\\Downloads\\" + filename);
}

I got that method from this documentation here but I want to download the download to show in the browser, and go to the standard downloads folder. 我从这个文档这种方法在这里 ,但我想下载的下载在浏览器中显示,并进入标准的下载文件夹中。 I created this method previously for downloading csv files: 我之前创建了用于下载csv文件的方法:

var csv = new StringBuilder();
csv.AppendLine("col1,col2,col3");

var bytes = Encoding.UTF8.GetBytes(csv.ToString());

var response = new FileContentResult(bytes, "text/csv");
response.FileDownloadName = fileName;
return response;

I tried doing the same thing with the S3 file: 我尝试对S3文件执行相同的操作:

GetObjectResponse response = client.GetObject(request);
// response.WriteResponseStreamToFile("\\Downloads\\" + filename);
var bytes = Encoding.UTF8.GetBytes(response.ToString());
var download = new FileContentResult(bytes, "application/pdf");
download.FileDownloadName = filename;
return download;

it downloads a file, but the pdf doesn't work (it fails to load). 它会下载文件,但pdf无法正常工作(无法加载)。 How do I download the file that I got from S3? 如何下载从S3获得的文件?

you would need to do something like the following 您将需要执行以下操作

int byteCount
byte[] buffer = new byte[2048]; // read in chunks of 2KB
Stream responseStream= Response.GetResponseStream();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);

while ((byteCount = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    Response.OutputStream.Write(buffer, 0, byteCount);    
}

Response.Flush();
Response.Close();
Response.End();

Here's the working solution I came up with: 这是我想出的可行解决方案:

GetObjectResponse response = client.GetObject(request);
using (Stream responseStream = response.ResponseStream)
{
    var bytes = ReadStream(responseStream);
    var download = new FileContentResult(bytes, "application/pdf");
    download.FileDownloadName = filename;
    return download;
}

public static byte[] ReadStream(Stream responseStream)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM