• 附件删除与下载


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Text;

    /// <summary>
    ///DownLoad 的摘要说明
    /// </summary>
    public class FileHelper
    {
        public FileHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 文件上传到的路径
        /// </summary>
        /// <param name="uploadPath"></param>
        public void FileUpload(string uploadPath)
        {
            try
            {
                HttpFileCollection files = HttpContext.Current.Request.Files;      //客户端上传控件集合
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    if (file.ContentLength > 0)
                    {
                        string fileName = file.FileName;    //文件名
                        string fileType = "";               //文件类型

                        if (fileName.Length > 0)
                        {
                            fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                            fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                        }
                        string uniquelyFileName = Guid.NewGuid().ToString() + "." + fileType;  //文件唯一名字
                        file.SaveAs(uploadPath + uniquelyFileName);
                        #region......保存信息到数据库

                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("Error:" + ex.Message);
            }
        }
        /// <summary>
        ///  文件下载
        /// </summary>
        /// <param name="filePath">完整路径包含文件名</param>
        /// <param name="fileName">下载后的文件名包含后缀名</param>
        public void DownLoadEvent(string filePath, string fileName)
        {

            try
            {
                string browser = HttpContext.Current.Request.UserAgent.ToUpper();
                string outputFileName = "";
                FileStream fileStream = new FileStream(filePath, FileMode.Open);
                long fileSize = fileStream.Length;
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                if (browser.Contains("MS") == true && browser.Contains("IE") == true)  //ie浏览器
                {
                    outputFileName = HttpUtility.UrlEncode(fileName);
                }
                else if (browser.Contains("FIREFOX") == true)    //firefox浏览器
                {
                    //outputFileName = "/" + fileName + "/";
                    outputFileName = fileName;
                }
                else  //其他浏览器
                {
                    outputFileName = HttpUtility.UrlEncode(fileName);
                }
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + outputFileName);
                HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());

                byte[] fileBuffer = new byte[fileSize];
                fileStream.Read(fileBuffer, 0, (int)fileSize);
                HttpContext.Current.Response.BinaryWrite(fileBuffer);

                fileStream.Dispose();
                fileStream.Flush();
                fileStream.Close();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("Error:" + ex.Message);
            }
        }
    }

    =============================================================

    调用方法

    /// <summary>
        /// 下载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DownLoadEvent(object sender, EventArgs e)
        {
            string str = e.CommandArgument.ToString();
            fileHelper.DownLoadEvent(Server.MapPath("file/新建文本文档.txt"), "新建文本文档.txt");
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FileUploadEvent(object sender, EventArgs e)
        {
            fileHelper.FileUpload(Server.MapPath("File\\"));
        }

  • 相关阅读:
    Alpha版使用说明
    团队绩效评估计划
    丹佛机场行李处理系统分析
    第一个Spring冲刺周期团队进展报告
    用户体验
    总结
    Beta版
    Alpha版使用说明书
    5-26课堂作业——组员投票Alpha版存在的问题
    冲刺周期二--站立会议07
  • 原文地址:https://www.cnblogs.com/ajun/p/2523856.html
Copyright © 2020-2023  润新知