zoukankan      html  css  js  c++  java
  • 附件删除与下载

    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\\"));
        }

  • 相关阅读:
    COGS2355 【HZOI2015】 有标号的DAG计数 II
    COGS2353 【HZOI2015】有标号的DAG计数 I
    COGS2259 异化多肽
    二项式定理
    Codeforces 528D Fuzzy Search
    技术文章阅读-华为WS331a产品管理页面存在CSRF漏洞
    技术文章阅读-天翼创维awifi路由器存在多处未授权访问漏洞
    技术文章阅读-红蓝对抗系列之浅谈蓝队反制红队的手法一二
    技术文章阅读-Solr ReplicationHandler漏洞浅析
    技术文章阅读-记一次edu站点从敏感信息泄露到getshell
  • 原文地址:https://www.cnblogs.com/ajun/p/2523856.html
Copyright © 2011-2022 走看看