Skip to content

Instantly share code, notes, and snippets.

@liangchaoboy
Forked from Dillie-O/gist:2480125
Created October 16, 2019 14:33
Show Gist options
  • Save liangchaoboy/53debed20204a1da0b9674f23dcf65a5 to your computer and use it in GitHub Desktop.
Save liangchaoboy/53debed20204a1da0b9674f23dcf65a5 to your computer and use it in GitHub Desktop.
Amazon S3 Download File with Prompt
public static void DownloadObject(string keyName)
{
string[] keySplit = keyName.Split('/');
string fileName = keySplit[keySplit.Length - 1];
string dest = Path.Combine(HttpRuntime.CodegenDir, fileName);
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client())
{
GetObjectRequest request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);
using (GetObjectResponse response = client.GetObject(request))
{
response.WriteResponseStreamToFile(dest, false);
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.TransmitFile(dest);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
// Clean up temporary file.
System.IO.File.Delete(dest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment