其实C#的文件上传是非常简单的
前台代码
<asp:FileUpload ID="FileUpload1" accept=".jpg,.png,.jpeg" runat="server" /> //这个就是选择文件类型,当然我这里accept写的是图片的,可以进行修改 <asp:Button ID="Button1" runat="server" Text="上传" /><br /> //上传按钮 <asp:Image ID="Image1" runat="server" /> //展示的图片
后台代码
string path = "UploadsImg/" + DateTime.Now.ToString("yyyyMMddHHmmssms") + FileUpload1.FileName; // 上传的图片防止重名 //制作画布,将选中的图片流制作成画布 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); //创建绘制对象,指向到画布上 Graphics g = Graphics.FromImage(img); PointF p = new PointF(10, 20); System.Drawing.Image logoimg = System.Drawing.Image.FromFile(Server.MapPath("UploadsImg/logo.png")); g.DrawImage(logoimg, p); //string s = "WWW.ITNBA.COM"; //Font f = new Font("微软雅黑", 55); //Brush b = new SolidBrush(Color.Red); //PointF p = new PointF(10, 20); ////绘制 //g.DrawString(s, f, b, p); img.Save(Server.MapPath(path)); Image1.ImageUrl = path; //FileUpload1.SaveAs(Server.MapPath(path));