一般处理程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
namespace web
{
/// <summary>
/// iputDecoment 的摘要说明
/// </summary>
public class iputDecoment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
String Name = context.Request["docName"];
String Nsg = context.Request["DocMsg"];
HttpPostedFile docWords = context.Request.Files["Decoment"];
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(docWords.FileName);//有bug的,一毫秒内多个人上传多个文件
string path = context.Server.MapPath("~/Doc/" + filename);
docWords.SaveAs(path);
context.Response.Write("Hello World");
//SqlHelper.ExecuteNonQuery("Insert into T_Products(Name,CategoryId,ImagePath,Msg) values(@Name,@CategoryId,@ImagePath,@Msg)", new SqlParameter("@Name", name), new SqlParameter("@CategoryId", categoryId), new SqlParameter("@ImagePath", "/uploadfile/" + filename), new SqlParameter("@Msg", msg));
//context.Response.Redirect("ProductList.ashx");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
HTML文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>文件上传</title>
</head>
<body>
<form action="../iputDecoment.ashx" method="post" enctype="multipart/form-data">
<h1>文件上传</h1> <br />
文件名:<input type="text" id="DocName" name="docName" "/><br />
上传文件:<input type="file" name="Decoment" /><br />
文件描述:<br />
<textarea name="DocMsg" cols="20" rows="5"></textarea>
<input type="submit" value="提交" name="Save" />
<input type="reset" value="重置" name="Save" />
</form>
</body>
</html>
需注意的是必须记得加入:enctype="multipart/form-data"
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px; color: red;">enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。 </span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">form里面的input的值以2进制的方式传过去,所以request就得不到值了。</span>