if (!string.IsNullOrEmpty(context.Request.Form["ispostback"]))//不为空,说明是点击上传 { if (context.Request.Files.Count > 0) { for(int i=0;i<context.Request.Files.Count;i++) { HttpPostedFile file = context.Request.Files[i]; file.SaveAs(context.Server.MapPath("upload\\" + file.FileName)); context.Response.Write("上传成功"); } } } else { string path = context.Server.MapPath("up.htm"); string strHTML = System.IO.File.ReadAllText(path); context.Response.Write(strHTML); }
ASP.net中对文件的上传非常方便。
如果对图片进行操作,例如打水印;
context.Response.ContentType="image/jpg"; string path=context.Server.MapPath("upload\\1.jpg");//转成物理路径 using (Image img = Bitmap.FromFile(path))//Bitmap图像操作类,可以剪切,缩放,打水印 { using (Graphics g = Graphics.FromImage(img))//从图像上面创建新的画笔 { g.DrawString("图片水印",new Font("黑体",16),Brushes.Black,new Point(0,0)); //文字,字体和大小,画笔颜色,位置 img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); } }