zoukankan      html  css  js  c++  java
  • 文件的上传和下载——FileUpload控件的使用

    1、文件的上传

    在Html页面拖一个FileUpload控件和一个Button按钮。

    后台编写代码如下:

    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class UpAndDown_UpLoadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload.HasFile)
            {
                var fileName = this.FileUpload.FileName;
                var fileType = this.FileUpload.PostedFile.ContentType;
                string filePath = null;
                long fileSize =SaveFile(this, this.FileUpload.FileContent, fileName, out filePath);
            }
        }
        public static long SaveFile(Page page, Stream file, String fileName, out String filePath)
        {
            long fileSize = 0;
            string appPath = page.Request.PhysicalApplicationPath;
            string saveDir = @"\SaveFile\";
            string savePath = appPath + saveDir;
            if (!Directory.Exists(savePath + DateTime.Now.ToString("yyyyMMdd") + "\\"))
            {
                Directory.CreateDirectory(savePath + DateTime.Now.ToString("yyyyMMdd"));
            }

            filePath = DateTime.Now.ToString("yyyyMMdd") + "\\" + fileName;
            if (System.IO.File.Exists(savePath + filePath))//如果文件已经存在,替换原文件
            {
                File.Delete(savePath + filePath);
            }
            using (FileStream writer = new FileStream(savePath + filePath, FileMode.CreateNew))
            {
                byte[] buf = new byte[4096];
                while (true)
                {
                    var r = file.Read(buf, 0, buf.Length);
                    if (r <= 0)
                    {
                        break;
                    }
                    else
                    {
                        fileSize += r;
                        writer.Write(buf, 0, r);
                        writer.Flush();
                    }
                }
                writer.Close();
            }
            return fileSize;
        }
    }

    2、文件的下载

    可以封装成一个通用类:

    public static void DownFile(Page page,string fileName)
        {
            string strPath = page.Server.MapPath("~/SaveFile/" + fileName);
            if (System.IO.File.Exists(strPath))
            {
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AppendHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                page.EnableViewState = false;
                HttpContext.Current.Response.WriteFile(strPath);
                HttpContext.Current.Response.End();
            }
            else
            {
                page.Response.Write("下载的文件不存在,请联系管理员");
            }
        }

    爱开发,爱分享,我是淑女,我是菜鸟程序员,请多指教喽!(*^__^*) 嘻嘻……
  • 相关阅读:
    JS模块化规范CMD之SeaJS
    JavaScript中判断变量类型最简洁的实现方法以及自动类型转换(#################################)
    Flex 弹性布局(****************************************************)
    使用jqMobi开发app基础:弹出内容的设计
    UVA 12232
    MVC5 Entity Framework学习之Entity Framework高级功能
    员工对什么不惬意
    有关Oracle cvu和cvuqdisk
    从Android Handler内部类到WeakReference的知识关联
    把Storyboard减轻的方法
  • 原文地址:https://www.cnblogs.com/SweetyGirl/p/3040970.html
Copyright © 2011-2022 走看看