Azure功能-从3D派对请求图像,然后将图像发送到请求者而不保存到本地目录

我发现了许多有关下载图像的问题,正如我的代码所示,这就是我最终要做的事情。但是,那不是我想要的行为。我只希望它直接返回图像。

using System.Net;
using microsoft.Extensions.Logging;
using System.IO;

public static async Task<HttpResponseMessage> Run(HttpRequest req,ILogger log,string data) 
{
    log.LogInformation("start function...");
    string qrData = $"{data}";//req.Query["id"];
    string QrGeneratorUrl = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&data="+ qrData;
    log.LogInformation("QrUrl= " + QrGeneratorUrl);

    var filename = "temp.png";
    var filePath = Path.Combine(@"d:\home\site\wwwroot\QrGeneratorTest\"+filename);

    WebClient myWebClient = new WebClient();
    myWebClient.DownloadFile(QrGeneratorUrl,filePath);

    var response = new HttpResponseMessage(HttpStatusCode.OK);
    var fileStream = new FileStream(filePath,FileMode.Open);
    response.Content = new StreamContent(fileStream);
    return response;
}

我尝试将图像转换为字节流并将流添加到响应内容中,我尝试将图像数据直接作为字符串内容放置...似乎无济于事-它仅在传输图像时发送是本地文件,我通过fileStream将其添加到响应中。有人知道我如何才能将收到的回复放入返回的回复中吗?或解释为什么不能完成?这是网络应用程序中存在的功能,我们正在尝试将其转移到功能中,并且该网络应用程序能够传递内容而不保存内容。使用字节流。但是我似乎无法在函数中复制它。

有两个原因导致我们没有直接致电qr服务器 1)这是一个3d派对网站,因此它可能会崩溃,我们需要能够将其从一个位置换成新的提供商。 2)我们需要构建url,以便它没有参数(?p = 1&q = 2&r = 3 ...),因为这将进入电子邮件,并且有一堆参数通常会将电子邮件标记为垃圾邮件。使用Azure(与我们的Web应用程序一样),我们可以构建如下网址:/ getImage / 1/2/3,该网址不太可能被标记为垃圾邮件

任何见识将不胜感激!

// ******************* // 回答 这是我的最终代码。我认为问题是Stream vs MemoryStream ...无论如何,这是完整代码:

#r "Newtonsoft.Json"

using System.Net;
using System;
using system.web;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

public static async Task<HttpResponseMessage> Run(HttpRequest req,string data) 
{
    log.LogInformation("start function...");
    string qrData = $"{data}";
    //string qrData = DateTime.Now.Ticks.ToString();
    string QrGeneratorUrl = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&qzone=2&data="+ qrData;

    //get the QR image from 3d party api
    var httpWebRequest = WebRequest.Create(QrGeneratorUrl);
    var httpResponse = await httpWebRequest.GetResponseAsync();

    //put 3d party response into function response
    Stream ms = httpResponse.GetResponseStream(); //new MemoryStream(bytes);
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(ms);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

    return result;
}
laosizhxy 回答:Azure功能-从3D派对请求图像,然后将图像发送到请求者而不保存到本地目录

假设您要下载图像以流式传输并只返回它(如果使用浏览器发送请求,请在浏览器中显示图像,如果我弄错了,请告诉我)。如果这是您的目的,您可以参考下面的代码,我从blob下载图像并将其返回到FileContentResult

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.DrawingCore;

namespace FunctionApp72
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Function,"get","post",Route = null)] HttpRequest req,ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            CloudStorageAccount blobAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            CloudBlobClient blobClient = blobAccount.CreateCloudBlobClient();

            CloudBlobContainer blobContainer = blobClient.GetContainerReference("test");
            CloudBlockBlob cloudBlockBlob = blobContainer.GetBlockBlobReference("test.jpg");
            MemoryStream streamIn = new MemoryStream();
            await cloudBlockBlob.DownloadToStreamAsync(streamIn);

            Image originalImage = Bitmap.FromStream(streamIn);
            return new FileContentResult(ImageToByteArray(originalImage),"image/jpeg");

        }

        private static byte[] ImageToByteArray(Image image)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(image,typeof(byte[]));
        }
    }
}

我将其部署到天蓝色,它仍然可以返回图像。

enter image description here

本文链接:https://www.f2er.com/3033969.html

大家都在问