步步为营-70-asp.net简单练习(文件的上传和下载)

大文件的上传一般通过FTP协议,而一般小的文件可以通过http协议来完成

1 通过asp.net 完成图片的上传

1.1 创建html页面

  注意:1 method="post" ;2 enctype="multipart/form-data"; 3 <input type="file" />  

<!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 method="post" action="FileUpload.ashx" enctype="multipart/form-data">
<input type="file" id="imgUpLoad" name="imgUpLoad" />
<input type="submit" value="提交" />
</form>
</body>
</html>

FileUpload.html

1.2 创建一般处理程序.ashx
  注意:1 创建文件保存路径

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

FileUpload.ashx

步步为营-70-asp.net简单练习(文件的上传和下载)

2 上传文件格式的验证,假设规定只能上传,gif的图片

  我们可以在HTML通过jQuery来进行验证,也可以在.ashx中进行验证

2.1 修改ashx文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//01-01 获取文件后缀名
string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
if (extName != ".gif" || extName != ".Gif")
{
context.Response.Write("请上传.gif图片");
return;
}
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

步步为营-70-asp.net简单练习(文件的上传和下载)

2.2 引入jQuery,修改HTML页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="http://localhost:62225/Script/jquery-1.7.1.min.js"></script>
<title></title>
<script>
$(function () {
$("form").submit(function () {
var fname = $("#imgUpLoad").val();
var extname = fname.substring(fname.lastIndexOf('.'));
if (extname != ".gif" || extname != ".Gif") {
alert("请上传.gif图片");
return false;
} });
});
</script>
</head>
<body>
<form method="post" action="FileUpload.ashx" enctype="multipart/form-data">
<input type="file" id="imgUpLoad" name="imgUpLoad" />
<input type="submit" value="提交" />
</form>
</body>
</html>

html

步步为营-70-asp.net简单练习(文件的上传和下载)

3 如果文件只放在Upload文件夹下,随着时间的增长,文件势必会越来越多不利于寻找,可以根据日期建立相应文件夹

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//01-01 获取文件后缀名
string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
if (extName != ".gif" && extName != ".GIF")
{
context.Response.Write("请上传.gif图片");
return;
}
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload\\");
//02-01 根据日期创建文件夹
DateTime dt = DateTime.Now;
savePath += dt.Year + "\\" + dt.Month + "\\" + dt.Day ;
if (!Directory.Exists(savePath))
{
//创建文件夹
Directory.CreateDirectory(savePath);
}
//02-02文 件名为当前时间 savePath += "\\"+ dt.ToString().Replace(':','-')+".gif";
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='" + savePath.Substring(savePath.IndexOf("Upload")) + "'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

步步为营-70-asp.net简单练习(文件的上传和下载)

4 文件下载

<!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>
<img src="" />
<a href="FileDownload.ashx?f=Upload/2017.rar">Upload/.rar</a>
<a href="FileDownload.ashx?f=Upload/2017-06-14%2017-25-19.gif">Upload/--%--.gif</a>
</body>
</html>

html

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileDownload 的摘要说明
/// </summary>
public class FileDownload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string f = context.Request["f"];
context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition","attachment;filename=\""+f+"\";"); context.Response.WriteFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,f));
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

步步为营-70-asp.net简单练习(文件的上传和下载)

上一篇:【转】wireshark基本用法及过虑规则


下一篇:make,makefile,cmake“暴力编译法”的个人经验或理解。