前端:
<form action="/ImageUpload.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" />
<input type="submit" value="上传文件" />
</form>
后台:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
HttpPostedFile file = context.Request.Files["imgFile"];
string extName = System.IO.Path.GetExtension(file.FileName);
if(extName!=".jpeg" && extName!=".gif" && extName!=".jpg" && extName!=".png" )
{
context.Response.Write("文件格式不合法!");
return;
}
string uploadImgName = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
file.SaveAs(context.Request.MapPath(uploadImgName));
context.Response.Write("上传成功!");
}