【问题标题】:How do I download a file using to the downloads folder with response.WriteResponseStreamToFile? (Amazon S3, C#)如何使用 response.WriteResponseStreamToFile 将文件下载到下载文件夹? (亚马逊 S3,C#)
【发布时间】:2015-12-09 00:07:05
【问题描述】:

我有一些 PDF 存储在 S3 中,我正在尝试创建一个可以下载它们的链接。这是我到目前为止的方法:

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

我从该文档here 中获得了该方法,但我想下载下载以在浏览器中显示,然后转到标准下载文件夹。我之前创建了这个方法来下载 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;

我尝试对 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;

它会下载一个文件,但 pdf 文件不起作用(无法加载)。如何下载从 S3 获得的文件?

【问题讨论】:

  • 加载失败是什么意思? acrobat 是不是读不出来?你能详细说明你得到的错误吗?
  • Acrobat 说它没有正确解码

标签: c# amazon-web-services amazon-s3 download


【解决方案1】:

这是我想出的可行解决方案:

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

【讨论】:

  • 很棒的解决方案!谢谢! 2021 年最适合我
【解决方案2】:

您需要执行以下操作

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

【讨论】:

  • Your_ByteCount 和缓冲区是什么?
  • GetResponseStream 抛出一个错误,说它是未定义的
  • 你有 pdf 所在位置的 url 吗?也许你可以使用 System.Net.WebClient 像这样 using (var wc = new WebClient()) { wc.DownloadFile(Url, FileName); } 来完成它,它还会抛出什么错误......
猜你喜欢
  • 2018-01-22
  • 2012-08-23
  • 1970-01-01
  • 2012-08-23
  • 2013-05-23
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
相关资源
最近更新 更多