C# 下载FTP文件(图片)

C# 下载FTP图片并在浏览器控件中显示

FTP图片不能直接利用 img 空间显示,必须先将图片下载到本地web应用中,在利用 img控件的src属性显示。

1.建立ftp连接下载图片

复制代码
 public void Download()
        {
            //获取平台路径,即:下载后的图片保存路径 Files/FTP/
            string uploadFilePath = "Files/FTP/";
            string filePath = Server.MapPath("/") + uploadFilePath.Replace("/", "\\");
       //判断是否存在此文件夹,如果不存在则创建文件夹
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
       //图片下载后的名称(可自己定义)
            string fileName = "20210417091239.jpg";
//FTP访问路径,我是在本地建立的FTP服务器。
string ftpPath = "ftp://192.168.5.101/20210417091239.jpg"; string sRet = "下载成功!"; FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create); // 根据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(); long cl = response.ContentLength; // 缓冲大小设置为2kb int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; // 每次读文件流的2kb readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { // 把内容从文件流写入 outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } //关闭两个流和ftp连接 ftpStream.Close(); outputStream.Close(); response.Close(); //向前台输出保存的文件(图片名称),便于img控件的src使用 returnValue = "{\"fileName\":\"" + "" + fileName + "\"}"; Response.Write(returnValue); Response.End(); } catch (Exception ex) { sRet = ex.Message; } }
复制代码

2.前台img控件接收保存后的图片路径

复制代码
function download(img) {
            //img可以是前端向后台传的ftp参数,我这里是ftp图片地址(根据自己需要传)
           
            var json = img;

            //后台下载ftp
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "Labor.ashx?Method=Download",
                data: {
                    json: json
                },
                beforeSend: function () {
                    load();//加载等待
                },
                complete: function () {
                    disLoad();
                },
                success: function (data) {

                    //src = "../Files/FTP/20210417091239.jpg"
                    var simg = '../Files/FTP/' + data.fileName;

                    $('#dahuikuimg').dialog({
                        title: '预览',
                        width: 600,
                        height: 750,
                        resizable: true,
                        closed: false,
                        cache: false,
                        modal: true
                    });
                    $("#simg").attr("src", simg);

                },
                error: function (data) {
            
                }
            });

        }
复制代码

3.前端需要img控件用于装载下载后的图片

  <div id='dahuikuimg'><img id="simg" /></div>

 

posted @   创客未来  阅读(1406)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示