本文整理匯總了C#中System.Web.HttpPostedFileBase.IsImage方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpPostedFileBase.IsImage方法的具體用法?C# HttpPostedFileBase.IsImage怎麽用?C# HttpPostedFileBase.IsImage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Web.HttpPostedFileBase
的用法示例。
在下文中一共展示了HttpPostedFileBase.IsImage方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: RegisterPost
public ActionResult RegisterPost(HttpPostedFileBase Photo)
{
var item = new Student() { UserRole = AccountStatus.Student };
try
{
TryUpdateModel(item, "RegisterItem", new string[] { "Login", "Name", "Password", "Group", "Course", "Age" });
if (db.Users.Count(i => i.Login == item.Login) > 0)
throw new ArgumentException("Пользователь с таким e-mail уже существует.");
if (ModelState.IsValid)
{
if (Photo != null && Photo.IsImage())
{
item.PhotoMime = Photo.ContentType;
item.Photo = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.PhotoMime);
Photo.InputStream.Read(item.Photo, 0, Photo.ContentLength);
}
db.Students.Add(item);
db.SaveChanges();
System.Web.HttpContext.Current.Session.Authorize(item);
return RedirectToAction("Profile");
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View(new AccountViewModel(item));
}
示例2: AdvertCreate
public ActionResult AdvertCreate(Advert model,HttpPostedFileBase image)
{
var cats = MongoDbContext.db.GetCollection<Category>("Category").Find(n => true).ToListAsync().GetAwaiter().GetResult();
ViewBag.CategoryId = new SelectList(cats, "Id", "Name");
//attempt to add the advert
if(ModelState.IsValid)
{
bool addSucceded;
try
{
if (image != null && image.IsImage())
{
image.SaveAs(Server.MapPath("~/Content/Images/") + image.FileName);
model.Photo = image.FileName;
}
else
model.Photo = null;
model.Id = ObjectId.GenerateNewId().ToString();
model.PublicationDate = DateTime.Now;
model.ViewNumber = 0;
//model.VIP = false;
model.UserId = MongoDbContext.getUser(User.Identity.Name).Id.ToString();
context.AddAdvert(model);
addSucceded = true;
}
catch (Exception e)
{
addSucceded = false;
}
if (addSucceded)
return RedirectToAction("AdvertCreate", new { Message = ManageMessageId.ChangePasswordSuccess });
else
ModelState.AddModelError("", "Ошибка добавления объявления");
}
// If we got this far, something failed, redisplay form
return View(model);
}
示例3: EditPost
public ActionResult EditPost(int? id, HttpPostedFileBase Photo)
{
Exhibit item = null;
try
{
if (id.HasValue)
{
item = db.Exhibits.SingleOrDefault(i => i.Id == id);
if (item == null)
throw new ArgumentException("Экспонат не найден.");
}
else
{
item = new Exhibit();
}
TryUpdateModel(item, "Item", new[] { "Name", "Description", "Location", "Code" });
if (ModelState.IsValid)
{
if (Photo != null && Photo.IsImage())
{
item.ApplicationType = Photo.ContentType;
item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
}
if (id == null)
db.Exhibits.Add(item);
else
db.Entry<Exhibit>(item).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View(new ExhibitViewModel(db, item));
}
示例4: ChangeImage
public ActionResult ChangeImage(HttpPostedFileBase Photo)
{
var user = db.Users.Find(System.Web.HttpContext.Current.Session.GetUser().Id);
if (Photo.IsImage())
{
user.PhotoMime = Photo.ContentType;
user.Photo = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(user.PhotoMime);
Photo.InputStream.Read(user.Photo, 0, Photo.ContentLength);
db.Entry(user).State = System.Data.EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Profile");
}
示例5: UploadPicture
public ActionResult UploadPicture(HttpPostedFileBase file, int albumId)
{
if (file == null)
{
throw new NullReferenceException("File is null!");
}
if (file.IsImage())
{
var image = file.HttpPostedFileBaseToImage().ImageToByteArray();
var albumPhoto = new AlbumPhoto()
{
Photo = image,
Likes = new List<Like>(),
Comments = new List<Comment>(),
AlbumId = albumId
};
this.Data.AlbumPhotos.Add(albumPhoto);
this.Data.SaveChanges();
}
else
{
throw new ArgumentException("File is not image");
}
return RedirectToAction("PhotosInAlbum", new { id = albumId });
}
示例6: Edit
public ActionResult Edit(Advert model, HttpPostedFileBase image)
{
var cats = MongoDbContext.db.GetCollection<Category>("Category").Find(n => true).ToListAsync().GetAwaiter().GetResult();
var old = MongoDbContext.db.GetCollection<Advert>("Advert").Find(m => m.Id == model.Id).FirstAsync().GetAwaiter().GetResult();
ViewBag.CategoryId = new SelectList(cats, "Id", "Name", model.CategoryId);
//attempt to edit the advert
if (ModelState.IsValid)
{
bool editSucceded;
try
{
if (image != null && image.IsImage())
{
image.SaveAs(Server.MapPath("~/Content/Images/") + image.FileName);
model.Photo = image.FileName;
}
else
model.Photo = old.Photo;
model.PublicationDate = old.PublicationDate;
model.ViewNumber = old.ViewNumber;
model.UserId = old.UserId;
//model.VIP = old.VIP;
MongoDbContext.db.GetCollection<Advert>("Advert").ReplaceOneAsync(m => m.Id == model.Id, model).GetAwaiter().GetResult();
editSucceded = true;
}
catch (Exception e)
{
editSucceded = false;
}
if (editSucceded)
return RedirectToAction("Edit", new { Message = ManageMessageId.Success });
else
ModelState.AddModelError("", "Ошибка добавления объявления");
}
// If we got this far, something failed, redisplay form
return View(model);
}
示例7: ChangeArticleImage
public ActionResult ChangeArticleImage(int id, HttpPostedFileBase Photo)
{
var item = db.Articles.Find(id);
if (item == null || item.Exhibit.Id == 0) //lazyloading fail
return RedirectToAction("Index");
if (Photo.IsImage())
{
if (Photo != null && Photo.IsImage())
{
item.ApplicationType = Photo.ContentType;
item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
}
db.Entry<Article>(item).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Articles", new { id = item.Exhibit.Id });
}
return RedirectToAction("Articles", new { id = item.Exhibit.Id });
}
示例8: EditArticlePost
public ActionResult EditArticlePost(int exhibitId, int? id, HttpPostedFileBase Photo)
{
Article item = null;
Exhibit exhibit = null;
try
{
exhibit = db.Exhibits.Find(exhibitId);
if (exhibit == null)
throw new ArgumentException("Экспонат не найден.");
if (id.HasValue)
{
item = db.Articles.SingleOrDefault(i => i.Id == id);
if (item == null)
throw new ArgumentException("Материал не найден.");
}
else
{
item = new Article() { Exhibit = exhibit };
}
TryUpdateModel(item, "Article", new[] { "Title", "Text" });
if (ModelState.IsValid)
{
if (Photo != null && Photo.IsImage())
{
item.ApplicationType = Photo.ContentType;
item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
}
if (id == null)
db.Articles.Add(item);
else
db.Entry<Article>(item).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Articles", new { id = exhibit.Id });
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View(new ExhibitViewModel(db, exhibit, item));
}
示例9: ChangeImage
public ActionResult ChangeImage(int id, HttpPostedFileBase Photo)
{
var item = db.Exhibits.Find(id);
if (item == null)
return RedirectToAction("Index");
if (Photo.IsImage())
{
if (Photo != null && Photo.IsImage())
{
item.ApplicationType = Photo.ContentType;
item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
}
db.Entry<Exhibit>(item).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}