上传文件 | 下载文件
HttpPostedFile 和 HttpPostedFileBase 你真的了解嘛?
上传文件
client:HtmlPage1.html
<!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>
<!--上传文件的时候:
1>必须使用Post方式来提交数据
2>必须设置enctype属性值为multipart/form-data,表示将数据以二进制的方式提交到server;
假设不设置的话enctype的默认值为application/x-www-form-urlencoded,表示将数据以键值对的方法是提交到server
-->
<form action="Handler1.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="fileName" value="" />
<input type="submit" value="上传"/>
</form>
</body>
</html>服务端:Handler1.ashx
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace 上传文件
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//设置报文头的content-type属性
context.Response.ContentType = "text/plain";
//获取浏览器发送过来的文件数据。
HttpPostedFile file = context.Request.Files["fileName"];
//获取上传的文件名称。
string fileName= file.FileName;
//int fileLength = context.Request.ContentLength; 事实上我们也能够获取文件的大小
//取一个新的名字(在旧文件名称前加上一个Guid就是为了防止上传的文件重名,产生冲突)
string newfileName = Guid.NewGuid().ToString() + "_" + fileName;
//将这个文件保存到项目下的files目录中
file.SaveAs(context.Server.MapPath("files")+"/" + newfileName);
context.Response.Write("OK");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
下载文件
client:HtmlPage1.html
<!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>
<a href="Handler1.ashx?fileName=123.jpg">123.jpg</a><br/>
<a href="Handler1.ashx?fileName=abc.txt">abc.txt</a><br />
<a href="Handler1.ashx?
fileName=word.docx">word.docx</a><br />
<a href="Handler1.ashx?fileName=介绍信模板.xls">介绍信模板.xls</a><br />
</form>
</body>
</html>
服务端:Handler1.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace 下载文件
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//获取用户要下载的资源名称
string filename = context.Request["fileName"];
//加入报文头(请參考:http://blog.csdn.net/fanbin168/article/details/38535735)
context.Response.AddHeader("Content-disposition", "attachment; filename=" +context.Server.UrlDecode( filename));
//获取到用户要下载的资源后。读取磁盘文件,把该文件发送给client
context.Response.WriteFile("~/Download/" + filename);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
使用ploadify插件进行文件上传
在视图页面中使用uploadify插件。进行文件上传 (uploadify插件去官网:http://www.uploadify.com去下载)
參考资料:Uploadify/uploadifive上传(中文文档)
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace WebApp.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="Filedata"></param>
/// <returns></returns>
public ActionResult FilesUpload(HttpPostedFileBase Filedata)
{
// 假设没有上传文件
if (Filedata == null ||string.IsNullOrEmpty(Filedata.FileName) ||Filedata.ContentLength == 0)
{
return this.HttpNotFound();
}
// 保存到 ~/zhaopian 目录中。名称不变
string filename = System.IO.Path.GetFileName(Filedata.FileName);
string virtualPath = string.Format("~/zhaopian/{0}", filename);
// 文件系统不能使用虚拟路径,所以这里须要将虚拟路径转换成物理路径
string path = this.Server.MapPath(virtualPath);
//保存文件
Filedata.SaveAs(path);
return this.Json(new { });
}
}
}视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>上传插件測试</title>
@Scripts.Render("~/bundles/jquery")
@Styles.Render("~/Content/css")
<link href="~/Scripts/uploadify/uploadify.css" rel="stylesheet" />
<script src="~/Scripts/uploadify/jquery.uploadify.js"></script>
</head>
<body>
<input id="file_upload" name="file_upload" type="file" multiple="true">
</body>
</html>
<script type="text/javascript">
$(function () {
$("#file_upload").uploadify({
swf: "/Scripts/uploadify/uploadify.swf", //指定上传控件的主体文件
uploader: "/Test/FilesUpload", //指定server端上传处理文件
width: 60, // button的宽度
height: 23, // button的高度
buttonText: "上传", // button上的文字
buttonCursor: 'hand', // button的鼠标图标
fileObjName: 'Filedata', // 上传參数名称
// 两个配套使用
fileTypeExts: "*.jpg;*.png", // 扩展名
fileTypeDesc: "请选择 jpg png 文件", // 文件说明
auto: true, // 选择之后。自己主动開始上传
multi: true, // 是否支持同一时候上传多个文件
queueSizeLimit: 5 // 同意多文件上传的时候,同一时候上传文件的个数
});
});
</script>
浙公网安备 33010602011771号