做了个图片上传的功能,最后发觉,不是图片格式都可以上传!请教大师如何处理!
void IAdvertisementRepository.UploadToDatabase(Advertisement advertisement) { HttpPostedFile Files = HttpContext.Current.Request.Files["Files"]; advertisement.Status = "On"; advertisement.CreatTime = DateTime.Now; Int32 Length = Files.ContentLength; byte[] TempImage = new byte[Length]; Files.InputStream.Read(TempImage, 0, Length); advertisement.ImageData = TempImage; db.Advertisements.Add(advertisement); db.SaveChanges(); }
1 HttpPostedFile Files = HttpContext.Current.Request.Files["Files"]; 2 3 var extension = Path.GetExtension(Files.FileName); 4 var allowExtentions = new string[] { ".jpg", ".png", ".gif" }; 5 6 if (allowExtentions.Contains(extension)) 7 { 8 //save 9 }
简单处理,限制下文件的拓展名吧~
能具体点吗?