Erica Stockwell-Alpert
Erica Stockwell-Alpert

Reputation: 4853

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. 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:

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:

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). How do I download the file that I got from S3?

Upvotes: 0

Views: 1933

Answers (2)

Erica Stockwell-Alpert
Erica Stockwell-Alpert

Reputation: 4853

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

Upvotes: 1

MethodMan
MethodMan

Reputation: 18843

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

Upvotes: 0

Related Questions