Пример #1
0
        /// <summary>
        /// 删除ftp中的文件
        /// </summary>
        /// <param name="ftpPath">ftp目录路径</param>
        /// <param name="remoteFileName">远程文件名</param>
        public static void DeleteFile(string ftpPath, string remoteFileName)
        {
            if (PublicClass.IsRegister && !PublicClass.IsSingleClent)
            {
                if (UIPublicClass.PublicClass.IsFTP || !PublicClass.strToInt(ftpPath))
                {
                    #region 从FTP删除原件

                    //获取ftpIP
                    if (FtpServerIP == string.Empty)
                    {
                        //if (getFtpIP() == string.Empty)
                        //{
                        MessageBox.Show("未能获取连接原件的配置信息\t\n\t\n删除远程文件失败!",
                                        "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                        //}
                    }

                    if (ftpPath != string.Empty && remoteFileName != string.Empty)
                    {
                        string FtpPath     = TopUploadURI + ftpPath; //ftp的路径
                        string FtpFileName = remoteFileName;         //ftp中保存的文件的名称

                        try
                        {
                            clsFTP cf = new clsFTP(new Uri(FtpPath), FtpUser, FtpPassword);

                            bool ishave = cf.FileExist(FtpFileName);
                            if (ishave)
                            {
                                cf.DeleteFile(FtpFileName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    #endregion
                }
                else
                {
                    #region 从数据库删除原件
                    try
                    {
                        DeleteYj(Convert.ToInt32(ftpPath));
                    }
                    catch { }
                    #endregion
                }
            }
            else
            {
                #region 本地存储

                if (ftpPath != string.Empty && remoteFileName != string.Empty)
                {
                    //string address = DBUtility.AppTempFilePath + "\\" + ftpPath + remoteFileName;
                    string address = CustomDirectory + "\\" + ftpPath + remoteFileName;
                    if (File.Exists(address))
                    {
                        File.Delete(address);
                    }
                }

                #endregion
            }
        }
Пример #2
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="ftpPath">文件存放在ftp中的路径</param>
        /// <param name="remoteFileName">文件在ftp中的文件名</param>
        /// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
        /// <param name="LocalFileName">保存本地的文件名</param>
        public static void DownLoadFile(string ftpPath, string remoteFileName, string LocalFilePath, string LocalFileName)
        {
            try
            {
                if (PublicClass.IsRegister && !PublicClass.IsSingleClent)
                {
                    if (UIPublicClass.PublicClass.IsFTP || !PublicClass.strToInt(ftpPath))
                    {
                        #region 从ftp下载原件

                        //获取ftpIP
                        if (FtpServerIP == string.Empty)
                        {
                            MessageBox.Show("未能获取查看原件的配置信息\t\n\t\n查看原件失败!",
                                            "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (ftpPath != string.Empty && remoteFileName != string.Empty)
                        {
                            string FtpPath     = TopUploadURI + ftpPath; //ftp的路径
                            string FtpFileName = remoteFileName;         //ftp中保存的文件的名称

                            try
                            {
                                clsFTP cf = new clsFTP(new Uri(FtpPath), FtpUser, FtpPassword);

                                //下载原价
                                cf.DownloadFile(FtpFileName, LocalFilePath, LocalFileName);
                            }
                            catch
                            {
                                Image img = UIPublicClass.ImagePublic.暂无图片;
                                img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }

                        #endregion
                    }
                    else if (UIPublicClass.PublicClass.IsWCF)
                    {
                        if (remoteFileName == string.Empty)
                        {
                            return;
                        }

                        string SaveFilePath = DBUtility.AppTempFilePath + "\\TempFile\\";//保存到本地的路径

                        if (!Directory.Exists(SaveFilePath))
                        {
                            Directory.CreateDirectory(SaveFilePath);
                        }
                        else
                        {
                            try
                            {
                                Directory.Delete(SaveFilePath, true);
                                Directory.CreateDirectory(SaveFilePath);
                            }
                            catch { }
                        }

                        //先判断本地是否有此原件,如有就直接打开
                        if (File.Exists(SaveFilePath + remoteFileName))
                        {
                            OpenLocalFile(SaveFilePath, remoteFileName);
                        }
                        else
                        {
                            try
                            {
                                Stream filestream = new MemoryStream();
                                UIPublicClass.WcfService.DownFile inValue = new UIPublicClass.WcfService.DownFile();
                                inValue.FileName     = remoteFileName;
                                inValue.SubDirectory = ftpPath;

                                var service = GetServiceProxy();
                                UIPublicClass.WcfService.DownFileResult retVal = service.DownLoadFile(inValue);
                                bool   issuccess = retVal.IsSuccess;
                                string message   = retVal.Message;
                                filestream = retVal.FileStream;
                                long filesize = retVal.FileSize;

                                if (issuccess)
                                {
                                    byte[]     buffer = new byte[filesize];
                                    FileStream fs     = new FileStream(SaveFilePath + remoteFileName, FileMode.Create, FileAccess.Write);
                                    int        count  = 0;
                                    while ((count = filestream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        fs.Write(buffer, 0, count);
                                    }

                                    //清空缓冲区
                                    fs.Flush();
                                    //关闭流
                                    fs.Close();
                                    //MessageBox.Show("下载成功!");
                                }
                                else
                                {
                                    Image img = UIPublicClass.ImagePublic.暂无图片;
                                    img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                            }
                            catch
                            {
                                Image img = UIPublicClass.ImagePublic.暂无图片;
                                img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                    else
                    {
                        #region 从数据库下载原件
                        MemoryStream ms = new MemoryStream();
                        ms = OpenYj(Convert.ToInt32(ftpPath));
                        if (ms != null)
                        {
                            FileStream   Fs = new FileStream(LocalFilePath + LocalFileName, FileMode.Create);
                            BinaryWriter Bw = new BinaryWriter(Fs);
                            Bw.Write(ms.ToArray());
                            Fs.Close();
                            Bw.Close();
                        }
                        else
                        {
                            Image img = UIPublicClass.ImagePublic.暂无图片;
                            img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        #endregion
                    }
                }
                else
                {
                    #region 本地存储

                    if (ftpPath != string.Empty && remoteFileName != string.Empty)
                    {
                        string from    = CustomDirectory + "\\" + ftpPath + remoteFileName;
                        string address = LocalFilePath + LocalFileName;
                        if (File.Exists(from))
                        {
                            File.Copy(from, address, true);
                        }
                        else
                        {
                            Image img = UIPublicClass.ImagePublic.暂无图片;
                            address = address.Substring(0, (address.Length - 4)) + ".jpg";
                            img.Save(address, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }