文件上传下载代码

using System;

using System.Web;

using System.IO;

using System.Text;

using System.Web.UI.WebControls;



namespace BD

{

    public class FileUtil

    {

        #region 文件路径



        /// <summary>

        /// 得到没有后缀的文件名

        /// </summary>

        /// <param name="_FullName"></param>

        /// <returns></returns>

        public static string GetFileNameWithoutExtension(string _FullName)

        {

            return Path.GetFileNameWithoutExtension(_FullName);

        }



        /// <summary>

        /// 得到文件的扩展名

        /// </summary>

        /// <param name="_FullName"></param>

        /// <returns></returns>

        public static string GetExtension(string _FullName)

        {

            return Path.GetExtension(_FullName);

        }



        /// <summary>

        /// 得到文件名

        /// </summary>

        /// <param name="_FullName"></param>

        /// <returns></returns>

        public static string GetFileName(string _FullName)

        {

            return Path.GetFileName(_FullName);

        }



        #endregion



        #region 文件上传下载删除



        /// <summary>

        /// 上传文件

        /// </summary>

        /// <param name="_DevicePath">需要保存到的根路径</param>

        /// <param name="_FileUpload">文件上传控件</param>

        /// <param name="_NewFileName">返回一个生成的文件名</param>

        /// <returns></returns>

        public static bool UpLoadFile(string _DevicePath, FileUpload _FileUpload, out string _NewFileName)

        {

            _NewFileName = string.Empty;

            StringBuilder sbPath = new StringBuilder();

            sbPath.Append(_DevicePath);

            //检查目录是否存在

            if (!Directory.Exists(sbPath.ToString()))

            {

                try

                {

                    //如果目录不存在就创建目录

                    Directory.CreateDirectory(sbPath.ToString());

                }

                catch (Exception)

                {

                    return false;

                }

            }

            //得到上传文件全路径

            string FullName = _FileUpload.PostedFile.FileName;

            //得到扩展名

            string FileExtension = GetExtension(FullName);

            //随机生成文件名

            Random random = new Random(DateTime.Now.Millisecond);

            string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + random.Next(int.MaxValue).ToString();

            _NewFileName = FileName;

            //增加随机生成的文件名

            sbPath.Append(FileName);

            //加上后缀名

            sbPath.Append(FileExtension);

            try

            {

                //上传到指定文件夹

                _FileUpload.PostedFile.SaveAs(sbPath.ToString());

            }

            catch (Exception)

            {

                return false;

            }

            return true;

        }



        /// <summary>

        /// 

        /// </summary>

        /// <param name="_Path"></param>

        public static void DownloadFileBinary(string _Path)

        {

            //新建一个只读的文件流

            FileStream fstream = new FileStream(_Path, FileMode.Open);

            //设置基本信息

            HttpContext.Current.Response.Buffer = false;

            HttpContext.Current.Response.AddHeader("Connection", "KeepAlive");

            HttpContext.Current.Response.ContentType = "image/jpeg";

            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(_Path));

            HttpContext.Current.Response.AddHeader("Content-Lengt", fstream.Length.ToString());

            while (true)

            {

                //开辟缓冲区空间

                byte[] buffer = new byte[1024];

                //读取文件的数据

                int leng = fstream.Read(buffer, 0, 1024);

                //到文件结尾结束

                if (leng == 0)

                    break;

                //读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入

                if (leng == 1024)

                    HttpContext.Current.Response.BinaryWrite(buffer);

                else

                {

                    //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块

                    byte[] b = new byte[leng];

                    for (int i = 0; i < leng; i++)

                        b[i] = buffer[i];

                    HttpContext.Current.Response.BinaryWrite(b);

                }

            }

            //关闭下载文件

            fstream.Close();

            //结束文件下载

            HttpContext.Current.Response.End();

        }



        /// <summary>

        /// 下载文件

        /// </summary>

        /// <param name="_Path"></param>

        public static void DownloadFile(string _Path)

        {

            FileInfo DownloadFile = new FileInfo(_Path);

            HttpContext.Current.Response.Clear();

            HttpContext.Current.Response.ClearHeaders();

            HttpContext.Current.Response.Buffer = false;

            HttpContext.Current.Response.ContentType = "application/x-msdownlad";

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));

            HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

            HttpContext.Current.Response.WriteFile(DownloadFile.FullName);

            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.End();

        }



        /// <summary>

        /// 删除文件

        /// </summary>

        /// <param name="_Path"></param>

        /// <returns></returns>

        public static bool DeleteFile(string _Path)

        {

            //判断文件是否存在

            if (File.Exists(_Path))

            {

                try

                {

                    File.Delete(_Path);

                }

                catch (Exception)

                {

                    return false;

                }

                return true;

            }

            return false;

        }



        #endregion

    }

}

 

你可能感兴趣的:(文件上传)