c# txt 文件上传、写入TXT文件、创建图形验证码
asp.net mvc 图片上传
html
在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性
<form enctype="multipart/form-data" action="upload" method="post" > <div> <input type="file" name="imgFile"> <input type="submit" value="上传"> </div> </form>
后台
public JsonResult upload(HttpPostedFileBase imgFile) { string da = DateTime.Now.ToString("yyyy-MM-ddHHmmss"); //获取虚拟路径 var fileName = Server.MapPath(@"~/assets/"); //获取文件后缀 string ExtName = Path.GetExtension(imgFile.FileName).ToLower(); //获取绝对路径 string imgPath = @"E:\img\" + da + ExtName; FileInfo file1 = new FileInfo(imgPath); //获取父目录路径 var di1 = file1.Directory; //判断父目录是否存在 if (!di1.Exists) di1.Create(); //判断文件是否存在 if (!file1.Exists) { //保存文件 imgFile.SaveAs(imgPath); } return Json("success:true"); }
js上传
$('#file_upload').click(function () { var imgFile = new FormData(); var files = $('#file')[0].files; if (files) { imgFile.append('imgFile', files[0]); } $.ajax({ cache: false, type: 'post', dataType: 'json', url: 'upload', data: imgFile, contentType: false, processData: false, success: function (data) { alert(1); } }); });
写入txt
public void WLog() { string da = DateTime.Now.ToString("yy-MM-dd"); //获取虚拟路径 var fileName = Server.MapPath(@"~/assets/"); //获取绝对路径 string absolute_path = @"E:\"; //拼接文件路径 string path = fileName + "log/" + da + ".txt"; //创建对文件的引用 FileInfo file = new FileInfo(path); //获取父目录路径 var di = file.Directory; //判断父目录是否存在 if (!di.Exists) di.Create(); //判断文件是否存在 if (!file.Exists) { //创建文件 并释放文件资源 FileStream fs = file.Create(); fs.Close(); fs.Dispose(); } //写入流、释放资源 StreamWriter sw = file.AppendText(); //追加数据; sw.Write(" 修改时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ",删除号: 1111,被修改的账户:222,登录名:3333,账号所登录的IP:1232546\r\n"); sw.Close(); sw.Dispose(); }
创建图形验证码
/// <summary> /// 创建图形验证码 /// </summary> /// <param name="validateNum">验证码长度</param> private void CreateImage(string validateNum) { if (validateNum == null || validateNum.Trim() == string.Empty) return; //生成BitMap图像 Bitmap image = new Bitmap(validateNum.Length * 10 + 12, 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景 g.Clear(Color.White); ////画图片的背景噪音线 //for (int i = 0; i < 25; i++) //{ // int x1 = random.Next(image.Width); // int x2 = random.Next(image.Width); // int y1 = random.Next(image.Height); // int y2 = random.Next(image.Height); // g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2); //} Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(validateNum, font, brush, 2, 2); //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); //将图像保存到指定流 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }