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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM