?
1 2 3 4 | 前台页面:< BR >< form action = "upload.ashx" method = "post" enctype = "multipart/form-data" >
< input type = "file" name = "txtUpload" id = "fFile" />
< input type = "submit" value = "上传" id = "btnUpload" />
</ form >
|
一个file的input标签,一个表单提交按钮,将以post的形式提交到一般处理程序进行处理。
uploas.ashx:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain" ;
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];
if (! string .IsNullOrEmpty(file.FileName))
{
string extention = Path.GetExtension(file.FileName);
string name = DateTime.Now.ToString( "yyyyMMdd" ) + new Random().Next(1000, 10000) + extention;
string path = context.Server.MapPath( "Uploads/" + name);
file.SaveAs(path);
context.Response.Write( "ok" );
}
}
}
|