从宁静的Web服务下载图像并使用asp.net MVC在剃须刀中显示

时间:2018-11-11 14:32:31

标签: asp.net-mvc webclient

我尝试从URL获取图像并以剃刀显示图像。但是我无法将字节更改为图像并显示此图像。

//this is a string contains 3name of image and i give each of them with this 
   way
  string[] imgname = user.Original_images.Split(',');
        foreach (var item3 in imgname)
        {
            string url2 = "my URL" + 
             + item3 + ".jpg";

           //i download image successfully
            webclient.Headers["Authorization"] = "Basic " + 
              Convert.ToBase64String( 
              Encoding.Default.GetBytes("Username:password"));

            byte[] data = webclient.DownloadData(url2);

           //i don't know what should i do after this...
        }

2 个答案:

答案 0 :(得分:1)

您可以使用FileResult操作:

请记住,一次只能返回一个文件结果,可以使用多个控制器调用多个图像。

//changed part
return File(data , System.Net.Mime.MediaTypeNames.Application.Octet, item3);    

public FileResult Download()
{
    //this is a string contains 3name of image and i give each of them with this 
   way
  string[] imgname = user.Original_images.Split(',');
        foreach (var item3 in imgname)
        {
            string url2 = "my URL" + 
             + item3 + ".jpg";

           //i download image successfully
            webclient.Headers["Authorization"] = "Basic " + 
              Convert.ToBase64String( 
              Encoding.Default.GetBytes("Username:password"));

            byte[] data = webclient.DownloadData(url2);

           //i don't know what should i do after this...
            return File(data , System.Net.Mime.MediaTypeNames.Application.Octet, item3);
        }


}

答案 1 :(得分:0)

我解决了我的问题

Modules

在剃刀中我有这个

 string[] imgname = user.Original_images.Split(',');
    foreach (var item3 in imgname)
    {
        string url2 = "my URL" + 
         + item3 + ".jpg";

       //i download image successfully
        webclient.Headers["Authorization"] = "Basic " + 
          Convert.ToBase64String( 
          Encoding.Default.GetBytes("Username:password"));

        byte[] data = webclient.DownloadData(url2);

       //this is new
            string imageBase64Data = Convert.ToBase64String(imageByteData);
            string imageDataURL = string.Format("data:image/jpg;base64,{0}", 
            imageBase64Data);
            ViewBag.ImageData = imageDataURL; 
    }