从外部域(AWS S3)加载图像并将其存储在浏览器内存中

4
我刚开始学习ASP.net,想知道通过使用Amazon S3的过期链接从外部域加载照片并将照片存储在浏览器内存中以供另一个脚本使用OpenBinary方法时是否容易?这可以使我在打印到屏幕之前调整大小和加水印。
这是我想要实现的:
在loadImage.aspx上,我从我的数据库获取photoID,为Amazon S3创建一个过期的签名URL,调用照片并将其保存到内存中。当在内存中时,我的ASP.Jpeg脚本将调用OpenBinary方法,调整大小和加水印,然后使用SendBinary方法显示照片。
我认为MemoryStream或Response Binary Write可能是我正在寻找的东西,但不确定如何在外部照片源上使用它。这是我迄今为止所做的事情,但因为不确定能否在内存中加载外部域照片,是否遗漏了重要信息,感到困惑,所以我需要帮助。 我的图像元素:
<img src="loadImage.aspx?p=234dfsdfw5234234">

在loadImage.aspx页面上:

string AWS_filePath = "http://amazon............"

using (FileStream fileStream = File.OpenRead(AWS_filePath))
{
    MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}

// Persits ASP.Jpeg Component

objJpeg.OpenBinary( ... );
// resize bits
// watermark bits
objJpeg.SendBinary( ... );

任何帮助都将是惊人的。
1个回答

5

首先要使用处理程序.ashx而不是完整的.aspx页面。处理程序没有所有aspx页面的调用,更清晰地了解您要发送的内容,并避免已存在的标头。

<img src="loadImage.ashx?p=234dfsdfw5234234">

如何下载图片。
string url = "http://amazon............"
byte[] imageData;
using (WebClient client = new WebClient()) {
   imageData = client.DownloadData(url);
}

如何将图片发送到浏览器
// this is the start call from the handler
public void ProcessRequest(HttpContext context)
{
    // imageData is the byte we have read from previous
    context.Response.OutputStream.Write(imageData, 0, imageData.Length);
}

如何设置缓存和头部信息
    public void ProcessRequest(HttpContext context)
    {
      // this is a header that you can get when you read the image
      context.Response.ContentType = "image/jpeg";
      // the size of the image
      context.Response.AddHeader("Content-Length", imageData.Length.ToString());
      // cache the image - 24h example
  context.Response.Cache.SetExpires(DateTime.Now.AddHours(24));
      context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0));
      // render direct
      context.Response.BufferOutput = false;

    ...
    }

希望这些技巧能帮助您前进。

相关:
https://stackoverflow.com/search?q=%5Basp.net%5D+DownloadData


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接