.NET获取FTP文件,返回JSON字符串

获取File文件JSON格式

private string GetFileJosnList()
{
    JSONHelper json = new JSONHelper();
    try
    {
        string ID = Request.Form["MID"];  //主键ID
        FTPUtil ftp = new FTPUtil(ftpServer, "", user, password);

        if (ftp.DirectoryExist("STK004"))  //是否存在此文件
        {
            ftp.GotoDirectory("STK004/" + ID + "/", true);
            string[] files = ftp.GetFileList("*.*");
            string[] files2 = ftp.GetFileList2("*.*");
            string[] file = null;
            string fileName = string.Empty;
            if (files != null && files.Length > 0)
            {
                int j = 0;
                foreach (string name in files)
                {
                    file = name.Split(DbUtil.rowSplit);
                    fileName = name;
                    if (file.Length == 1)
                    {
                        file = files2[j].Split(DbUtil.rowSplit);
                        fileName = files2[j];
                    }

                    //将数据写成json格式返回 key:value
                    json.AddItem2("FILE_SIZE", (ftp.GetFileSize(fileName)).ToString());
                    json.AddItem2("FILENAME", "<a href='" + fileServerPath + "STK004/" + ID + "/" + fileName + "' target='_blank'>" + file[1] + "</a>");
                    json.AddItem2("HIDFILENAME", fileName);
                    json.AddItem2("FILEURL", fileServerPath + "STK004/" + ID + "/" + fileName);
                    json.ItemOk();
                    j++;
                }
            }
            return json.ToString();
        }
        else
        {
            return DbUtil.EmptyJson;  //自定义报错json
        }
    }
    catch (Exception)
    {
        return DbUtil.EmptyJson;
    }
}

FTPUtil工具包 

public FTPUtil(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
    ftpServerIP = FtpServerIP;
    ftpRemotePath = FtpRemotePath;
    ftpUserID = FtpUserID;
    ftpPassword = FtpPassword;
    ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
}

public bool DirectoryExist(string RemoteDirectoryName)
{
    string[] dirList = GetDirectoryList();
    if (dirList != null)
    {
        foreach (string str in dirList)
        {
            if (str.Trim() == RemoteDirectoryName.Trim())
            {
                return true;
            }
        }
    }
    return false;
}

这里展示的不够完整具体,因为还有很多处理代码,自己也不是很理解...

猜你喜欢

转载自www.cnblogs.com/chuyunno1/p/10617456.html
今日推荐