jiaoye6789 2017-01-18 06:24 采纳率: 0%
浏览 4338
已采纳

前端调用webApi显示图片

以前MVC直接调用Controller里面的方法
VIEW

 $("#imgEntering").attr("src", "/Center/DownloadPic?ftpPath=" + obj[0].PIC_URL + "");

Controller

 public FileResult DownloadPic(string ftpPath)
        {

            FtpWebRequest reqFTP;
            try
            {
                // 根据uri创建FtpWebRequest对象   
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));

                // 指定执行什么命令  
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

                // 指定数据传输类型  
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;

                // ftp用户名和密码  
                //reqFTP.Credentials = new NetworkCredential();
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                // 把下载的文件写入流
                Stream ftpStream = response.GetResponseStream();

                // 缓冲大小设置为2kb  
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                MemoryStream mStream = new MemoryStream();
                //每次读文件流的2kb
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    //把内容从文件流写入   
                    //outputStream.Write(buffer, 0, readCount);
                    mStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                //关闭两个流和ftp连接
                ftpStream.Close();
                mStream.Close();
                response.Close();
                return File(mStream.ToArray(), "image/jpg");
            }
            catch (Exception ex)
            {
                byte[] file = new byte[0];
                return File(file, "image/jpg");
            }
        }

现在多了一个WEBAPI,前台调用WEBAPI读取图片,API返回图片流,直接在前台显示,求个解决方案

  • 写回答

2条回答 默认 最新

  • Go 旅城通票 2017-01-18 08:21
    关注

    MVC返回图片base64字符,而不是2进制数据流

    Asp.Net图片base64编码相互转换

    然后通过data:image/gif;base64加载

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组