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

标签 c# asp.net

我是 ASP.net 的新手,想知道从外部域 (Amazon S3) 加载照片是多么容易,使用它们的过期链接,并将照片存储在浏览器内存中以供另一个脚本使用一个 OpenBinary 方法?这允许我在打印到屏幕之前调整大小和水印。

这就是我想要发生的事情:

在 loadImage.aspx 上,我从我的数据库中获取照片 ID,为 Amazon S3 创建一个到期的签名 URL,以某种方式调用照片并将其保存到内存中。在内存中时,我的 ASP.Jpeg 脚本将调用 OpenBinary 方法,调整照片大小并为其添加水印,然后使用 SendBinary 方法显示照片。

我认为内存流或响应二进制写入可能是我正在寻找的东西,但不确定如何在外部照片源上使用它。到目前为止,这是我所做的,但我很困惑,我想我会得到帮助,因为我不确定这是否可行,如果我可以在内存中加载外部域照片,如果我遗漏了一些重要的东西…… ..

我的图像元素:

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

任何帮助都会很棒。

最佳答案

首先开始使用处理程序 .ashx 而不是完整的 .aspx 页面。处理程序没有 aspx 页面的所有调用,对于您要发送的内容更加清晰,并且您可以避免所有现成的现有 header 。

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

如何设置缓存和header

    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

关于c# - 从外部域 (AWS S3) 加载图像并将其存储在浏览器内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11590342/

相关文章:

asp.net - 从 SQLException 错误输出异常

asp.net - Request.ServerVariables ["HTTP_X_FORWARDED_FOR"] 和 Request.Headers ["X-Forwarded-For"] 之间有区别吗?

c# - 类似 JSON 的动态对象数组

c# - 使用 RSACryptoProvider 在 C# 中验证 PHP OpenSSL 签名

c# - 使用 AutoMapper 进行映射时,是否有一种有效的方法仅保留子集合的单个元素?

asp.net - 如何以require格式打印页面的gridview

c# - NHibernate Query to json 结果出乎意料

c# - 在单元测试中模拟 HTTPResponse

c# - ASP.NET 中的 Windows 时区缩写

c# - ASP.Net Web API Swashbuckle 如何忽略 HttpRequestMessage