文件的上传和下载——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("下载的文件不存在,请联系管理员");
        }
    }

posted @ 2013-04-24 21:08  丫丫是个小码农  阅读(701)  评论(7编辑  收藏  举报