public ActionResult DownloadSomeonesFile(int Id) { int points = UserProfileModel.GetUserPoints(); points -= 20; if (points < 0) { return(View("Error")); } byte[] bytes; string fileName, contentType; string constr = @"data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MyDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT FileName, FileContent, ContentType FROM UploadedFile Where FileId = @Id"; cmd.Parameters.AddWithValue("@Id", Id); cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes = (byte[])sdr["FileContent"]; contentType = sdr["ContentType"].ToString(); fileName = sdr["FileName"].ToString(); } con.Close(); } var query = "UPDATE UserProfile SET Points = @Points WHERE Email = @Email"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@Points", points); cmd.Parameters.AddWithValue("@Email", GetEmail()); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } return(File(bytes, contentType, fileName)); }
public void DeleteFile(int id) { var email = GetEmail(); string constr = @"data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MyDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "DELETE FROM UploadedFile WHERE FileId=@Id"; cmd.Parameters.AddWithValue("@Id", id); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } var query = "UPDATE UserProfile SET Points = @Points WHERE Email = @Email"; int points = UserProfileModel.GetUserPoints(); points -= 20; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@Points", points); cmd.Parameters.AddWithValue("@Email", email); con.Open(); points = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); } } Response.Redirect("~/UploadedFiles/Index"); }
// GET api/<controller> public int Get() { return(UserProfileModel.GetUserPoints()); }