图片上传
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//context.Request[]
//拿到上传来的文件
HttpPostedFile file = context.Request.Files["imgFile"];
//后台也要校验。
string ext = Path.GetExtension(file.FileName);
if (!(ext == ".jpeg" || ext == ".jpg" || ext == ".png" || ext == ".gif"))
{
//不是图片的时候:
context.Response.Write("shit");
context.Response.End();
}
else
{
string path = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
file.SaveAs(context.Request.MapPath(path));
string str = string.Format("<html><head></head><body><img src='{0}'/></body></html>", path);
//把图片显示给用户
context.Response.Write(str);
}
}
文件下载
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string encodeFileName =HttpUtility.UrlEncode("新建文本文档.txt");
context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", encodeFileName));
context.Response.WriteFile("新建文本文档.txt");
}