通过 <a>标签实现ftp文件下载功能

环境:

ftp登录用户名

ftp登录密码

ftp文件路径

一般处理程序

下面是我通过网上查找的资料实现的一个例子

首先页面中的a 标签拼接路径带需要的参数

<a class="downST" href="../ashx/Folder.ashx?strPath=ftp://[ftp ip地址]/PLM/File/标准ztree数据.txt&FileName=标准ztree数据.txt">下载</a>

下面是处理程序代码

 public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string strUserName = "****";    //ftp登录用户名
        string strPassword = "******.";  //ftp登录密码

            string FileName = context.Request["FileName"].ToString();
             string address = context.Request["strPath"].ToString();

        System.Net.WebClient request = new System.Net.WebClient();
        request.Credentials = new System.Net.NetworkCredential(strUserName, strPassword);//认证FTP用户名密码  
        byte[] newFileData = request.DownloadData(address );

        下载文件
        //DownLoadFile(strFileName, newFileData, HttpContext context1);
        
         context.Response.ContentType = "application/octet-stream";
        if (FileName == "")
        {
            FileName = "Temp";
        }
        FileName = ToHexString(FileName);
        context.Response.AddHeader("Content-Disposition", "attachment;FileName=" + FileName);
        if (newFileData != null && newFileData.Length > 0)
            context.Response.OutputStream.Write(newFileData, 0, newFileData.Length);
        else
            context.Response.BinaryWrite(new byte[1]);
        context.Response.End();
        
    }
 

    public static string ToHexString(string s)
    {
        char[] chars = s.ToCharArray();
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < chars.Length; index++)
        {
            bool needToEncode = NeedToEncode(chars[index]);
            if (needToEncode)
            {
                string encodedString = ToHexString(chars[index]);
                builder.Append(encodedString);
            }
            else
            {
                builder.Append(chars[index]);
            }
        }

        return builder.ToString();
    }

    private static bool NeedToEncode(char chr)
    {
        string reservedChars = "$-_.+!*'(),@=&";

        if (chr > 127)
            return true;
        if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
            return false;

        return true;
    }

    private static string ToHexString(char chr)
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        byte[] encodedBytes = utf8.GetBytes(chr.ToString());
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < encodedBytes.Length; index++)
        {
            builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
        }
        return builder.ToString();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

  

转载于:https://www.cnblogs.com/Vinkong/p/10059385.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值