上传本地文件到文件服务器

谁都会走 提交于 2020-01-17 15:34:00
      /// <summary>
        /// 上传本地文件到文件服务器
        /// </summary>
        /// <param name="strFileServerPath">文件服务器地址</param>
        /// <param name="StrLocalFileName">本地文件名,带文件完整路径</param>
        /// <param name="strNewFileName">上传至服务器后的文件名</param>
        /// <param name="isCheckFileType">是否需要检核文件类型</param>
        /// <param name="strRequireFileType">检核文件类型</param>
        /// <param name="isDeleteLocalFile">是否删除本地文件</param>
        public void upLoadFileToFileServer(string strFileServerPath, string StrLocalFileName, string strNewFileName, bool isCheckFileType, string strRequireFileType, bool isDeleteLocalFile)
        {
            string strRegisterDate = DateTime.Now.ToShortDateString();

            try
            {
                if (!File.Exists(StrLocalFileName))
                {
                    System.Windows.Forms.MessageBox.Show("本地文件不存在!", "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    return;
                }

                string strFileType = Path.GetExtension(StrLocalFileName).Substring(1).ToUpper();
                if (isCheckFileType)
                {
                    if (strFileType != strRequireFileType)
                    {
                        System.Windows.Forms.MessageBox.Show("文件类型不符,请确认文件类型!", "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                        return;
                    }
                }
                if (!Directory.Exists(strFileServerPath))
                {
                    Directory.CreateDirectory(strFileServerPath);
                }

                if (strNewFileName.Trim().Length > 0)
                {
                    strNewFileName = strFileServerPath + @"\" + strNewFileName;
                }
                else
                {
                    strNewFileName = strFileServerPath + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss");
                }

                //是否带有文件后缀名
                if (strNewFileName.Substring(strNewFileName.LastIndexOf(".") + 1) != strFileType)
                {
                    strNewFileName += "." + strFileType;
                }


                //File.Copy(StrLocalFileName, strNewFileName);
                FileStream inFileStream = new FileStream(StrLocalFileName, FileMode.Open, FileAccess.Read);
                FileStream outFileStream = new FileStream(strNewFileName, FileMode.OpenOrCreate);

                byte[] buf = new byte[inFileStream.Length];

                int byteCount;

                while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
                {
                    outFileStream.Write(buf, 0, byteCount);
                }
                inFileStream.Flush();
                inFileStream.Close();
                outFileStream.Flush();
                outFileStream.Close();

                //删除本地文件
                if (isDeleteLocalFile)
                {
                    if (!checkFileIsOccupied(StrLocalFileName))
                    {
                        DeleteFile(StrLocalFileName);
                    }

                    if (Directory.GetFiles(StrLocalFileName.Substring(0, StrLocalFileName.LastIndexOf(@"\"))).Length == 0)
                    {
                        if (checkFileIsOccupied(StrLocalFileName.Substring(0, StrLocalFileName.LastIndexOf(@"\"))))
                        {
                            Directory.Delete(StrLocalFileName.Substring(0, StrLocalFileName.LastIndexOf(@"\")));
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!