public ActionResult UploadImage(HttpPostedFileBase[] UploadedImages, string galleryId) { if (UploadedImages == null || UploadedImages.Length <= 0) throw new FileNotFoundException("No image supplied, please try again."); var message = string.Empty; foreach (var UploadedImage in UploadedImages) { if (UploadedImage.ContentType != "image/pjpeg" && UploadedImage.ContentType != "image/jpeg" && UploadedImage.ContentType != "image/png" && UploadedImage.ContentType != "image/gif") { message = "Not a valid file type."; } else if (UploadedImage.ContentLength > Constants.ImageSize) { message = "File size is too large, please limit this to 1MB."; } else { //var filename = new Guid() + ".png"; var image = new ImagesDto() { //Url = filename, UploadedBy = "Authorized User", GalleryId = Convert.ToInt32(galleryId) }; var imageId = GalleryQueries.UploadImage(image); if (imageId > 0) { var p = Server.MapPath("~") + @"Images\GalleryImages\"; new WebImage(UploadedImage.InputStream).Save(p + imageId + ".png", Constants.ImageType); message = "Images uploaded successfully"; } else { message = "There was some problem with images upload. Please try again."; } } } TempData["Message"] = message; return RedirectToAction("Edit", new { @id = galleryId }); }
public static int UploadImage(ImagesDto image) { var id = 0; using (var conn = Connection.GetConnection()) { conn.Open(); var createCommand = "INSERT INTO Images (Title,Description,Url,UploadedOn,UploadedBy, GalleryId) VALUES ('" + (image.Title != null ? image.Title.ToDbCleanString() : image.Title) + "', '" + (image.Description != null ? image.Description.ToDbCleanString() : image.Description) + "', '" + "', '" + DateTime.Now.ToString("M/d/yyyy hh:mm:ss tt") + "', '" + image.UploadedBy+ "', " + image.GalleryId + "); SELECT SCOPE_IDENTITY()"; using (var cmd = new SqlCommand(createCommand, conn)) { cmd.CommandType = CommandType.Text; id = Convert.ToInt32(cmd.ExecuteScalar()); } conn.Close(); } return id; }
private static ImagesDto ReadImage(SqlDataReader dr) { var i = new ImagesDto { GalleryId = Convert.ToInt32(dr["GalleryId"]), ImageId = Convert.ToInt32(dr["Id"]), Title = dr["Title"] != DBNull.Value ? dr["Title"].ToString() : string.Empty, Description = dr["Description"] != DBNull.Value ? dr["Description"].ToString() : string.Empty, //Url = dr["Url"] != DBNull.Value ? dr["Url"].ToString() : string.Empty, UploadedBy = dr["UploadedBy"] != DBNull.Value ? dr["UploadedBy"].ToString() : string.Empty, UploadedOn = dr["UploadedOn"] != DBNull.Value ? Convert.ToDateTime(dr["UploadedOn"].ToString()) : DateTime.MinValue }; return i; }
public static ImagesDto GetImageById(int imageId) { var image = new ImagesDto(); using (var conn = Connection.GetConnection()) { conn.Open(); using (var cmd = new SqlCommand("SELECT * FROM Images WHERE Id = " + imageId, conn)) { cmd.CommandType = CommandType.Text; var dr = cmd.ExecuteReader(); while (dr.Read()) { image = ReadImage(dr); } } conn.Close(); } return image; }