以前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返回图片流,直接在前台显示,求个解决方案