006-文件上传校验与直接下载

-》说明:使用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格式,而是在请求体中使用分隔符划分
-》限制:不允许上传可执行文件,只允许上传静态文件
例:只允许上传图片
客户端校验
服务器端校验
-》提高:将文件保存到对应年、月、日文件夹下,以方便遍历
动态创建路径
拼接路径信息

文件上传校验

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6     <script>
 7         window.onload = function() {
 8             document.getElementById('form1').onsubmit = function ()
 9             {
10                 //var f1= document.getElementById('f1');
11                 //var ext = f1.value.substring(f1.value.lastIndexOf('.'));
12                 //if (ext == '.jpg') {
13                 //    return true;
14                 //} else {
15                 //    alert('文件格式非法');
16                 //    return false;
17                 //}
18             };
19         };
20     </script>
21 </head>
22 <body>
23     <form method="POST" action="UploadTest.ashx" enctype="multipart/form-data" id="form1">
24         <input type="text" name="fileName"/>
25         <br/>
26         <input type="file" name="f1" id="f1"/>
27         <input type="submit" value="上传"/>
28     </form>
29 </body>
30 </html>
 1         public void ProcessRequest(HttpContext context)
 2         {
 3             //文本格式text/html
 4             context.Response.ContentType = "text/html";
 5             //获取文件名
 6             string fileName = context.Request["fileName"];
 7             //根据键接收文件
 8             HttpPostedFile file1 = context.Request.Files["f1"];
 9             //获取文件扩展名(IE浏览器获取绝对全路径)
10             string ext = Path.GetExtension(file1.FileName);
11             if (ext != ".jpg")
12             {
13                 context.Response.Write("文件格式非法");
14                 return;
15             }
16             //绝对路径
17             string path = context.Request.MapPath("/Uploads/");
18             //相对路径
19             string path2 = "/uploads/";
20             //向特定文件夹中进行保存
21             DateTime now = DateTime.Now;
22             path += now.Year + "/" + now.Month + "/" + now.Day + "/";
23             path2 += now.Year + "/" + now.Month + "/" + now.Day + "/";
24             //判断是否存在
25             if (!Directory.Exists(path))
26             {
27                 //没有则创建
28                 Directory.CreateDirectory(path);
29             }
30             //保存(路径+文件名+扩展名)
31             file1.SaveAs(path+fileName+ext);
32             //显示
33             context.Response.Write("<img src='"+path2+fileName+ext+"'/>");
34         }

文件直接下载

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6 </head>
 7 <body>
 8     <a href="Uploads/redgreen.rar">Uploads/redgreen.rar</a>
 9     <a href="Uploads/redgreen.swf">Uploads/redgreen.swf</a>
10     <a href="Uploads/sg1.jpg">show girl</a>
11     <hr/>
12     <a href="/DownloadTest.ashx?f1=Uploads/redgreen.rar">Uploads/redgreen.rar</a>
13     <a href="/DownloadTest.ashx?f1=Uploads/redgreen.swf">Uploads/redgreen.swf</a>
14     <a href="/DownloadTest.ashx?f1=Uploads/sg1.jpg">show girl</a>
15 </body>
16 </html>
 1         public void ProcessRequest(HttpContext context)
 2         {
 3             string f1 = context.Request["f1"];
 4             //指定为下载操作
 5             context.Response.ContentType = "application/octet-stream";
 6             //附加头信息,表示保存文件时的文件名
 7             context.Response.AddHeader("Content-Disposition", "attachment; filename=""+f1+"";");
 8             //输出文件
 9             context.Response.WriteFile(context.Request.MapPath(f1));
10         }
原文地址:https://www.cnblogs.com/ninghongkun/p/6275818.html