ASP.NET知识总结(5.文件上传 文件下载)

5、文件上传

-》说明:使用http协议只适合传输小文件,如果想传递大文件,则需要使用插件或者客户  端程序(使用ftp协议)

-》客户端操作

1》为表单添加属性:enctype="multipart/form-data"

2》在表单中添加控件:<input type="file" name="f1"/> 

3》表单必须使用post提交方式

-》服务器端操作

1》使用Request.Files属性获取文件对象

2》使用HttpPostedFile对象的SaveAs()方法保存

-》观察一下数据报文

当设置表单的enctype="multipart/form-data"属性后,不再是key-value格式,而是在请求体中使用分隔符划分

-》限制:不允许上传可执行文件,只允许上传静态文件

例:只允许上传图片

客户端校验

服务器端校验

-》提高:将文件保存到对应年、月、日文件夹下,以方便遍历

动态创建路径

拼接路径信息

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form action="UploadTest.ashx" method="post" enctype="multipart/form-data" id="form1">
        <input type="text" name="filename" />
        <br />
        <input type="file" name="f1" id="f1" />
        <input type="submit" value="上传" />

    </form>
</body>
</html>
public class UploadTest : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //根据键接收文件
            string filename = context.Request["filename"];
            HttpPostedFile file1 = context.Request.Files["f1"];

            //保存
            string path = context.Request.MapPath("/Uploads/");
            string path2 = "/uploads/";
            //file1.SaveAs(path+file1.FileName);
            //获取文件的扩展名
            string ext = Path.GetExtension(file1.FileName);
            if (ext != ".jpg")
            {
                context.Response.Write("文件格式不正确");
                return;
            }
            //向特定文件夹进行保存
            DateTime now = DateTime.Now;
            path += now.Year + "/" + now.Month + "/" + now.Day + "/";
            path2 += now.Year + "/" + now.Month + "/" + now.Day + "/";

            //判断该路径文件是否存在
            if (!Directory.Exists(path))
            {
                //创建
                Directory.CreateDirectory(path);
            }

            file1.SaveAs(path + filename + ext);

            //显示
            context.Response.Write("<img src='" + path2 + filename+ext + "'>");



        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

6、文件下载

-》使用超链接直接指定要下载的文件

能被浏览器解析的文会被显示

不能被浏览器解析的文件会被下载

-》实现:无论文件格式,都不使用浏览器显示,完成下载

指向一般处理程序,文件地址作为参数

修改响应头:ContentType = "application/octet-stream";

设置头信息:AddHeader("Content-Disposition", "attachment; filename="文件名";");

输出文件:context.Response.WriteFile(文件地址);

-》提示:如果中文文件名乱码,可以进行url编码

HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);

返回一个字符串,作为文件的名字

《实例:aspnet项目复习/第二天/t1_Upload_Download

<body>

<a href="Uploads/sg2.jpg">sg2</a><br />
<a href="Uploads/美女.jpg" >美女</a><br />
<a href="Uploads/redgreen.swf">Uploads/redgreen.swf</a><br />
<a href="Uploads/redgreen.rar">Uploads/redgreen.rar</a>
<hr />
<hr />
<hr />
<hr />
<a href="/DownloadTest.ashx?f1=Uploads/sg2.jpg">sg2</a><br />
<a href="/DownloadTest.ashx?f1=Uploads/美女.jpg">美女</a><br />
<a href="/DownloadTest.ashx?f1=Uploads/redgreen.swf">Uploads/redgreen.swf</a><br />
<a href="/DownloadTest.ashx?f1=Uploads/redgreen.rar">Uploads/redgreen.rar</a>

</body>

public class DownloadTest : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string f1=context.Request["f1"];

            //指定为下载操作
            context.Response.ContentType = "application/octet-stream";

            //附加头信息,表示保存文件时的文件名
            context.Response.AddHeader("Content-Disposition","attachment;filename=""+f1+"";");

            //输出文件
            context.Response.WriteFile(context.Request.MapPath(f1));
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
原文地址:https://www.cnblogs.com/fenger-VIP/p/4317877.html