标签:
<>
客户端: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,表示将数据以二进制的方式提交到服务器;
如果不设置的话enctype的默认值为application/x-www-form-urlencoded,表示将数据以键值对的方法是提交到服务器
-->
<form action="Handler1.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="fileName" value="" />
<input type="submit" value="上传"/>
</form>
</body>
</html>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;
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/fanbin168/article/details/47838859