/// <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;
            }
        }

版权声明:本文为6B23原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/6B23/p/12205686.html