Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. protected void UploadFile(object sender, EventArgs e)
  2. {
  3. string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
  4. string contentType = FileUpload1.PostedFile.ContentType;
  5. using (Stream fs = FileUpload1.PostedFile.InputStream)
  6. {
  7. using (BinaryReader br = new BinaryReader(fs))
  8. {
  9. byte[] bytes = br.ReadBytes((Int32)fs.Length);
  10. string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
  11. using (MySqlConnection con = new MySqlConnection(constr))
  12. {
  13. string query = "INSERT INTO foto(FileName, ContentType, Content, IdAlerta) VALUES (@FileName, @ContentType, @Content)";
  14. using (MySqlCommand cmd = new MySqlCommand(query))
  15. {
  16. cmd.Connection = con;
  17. cmd.Parameters.AddWithValue("@FileName", filename);
  18. cmd.Parameters.AddWithValue("@ContentType", contentType);
  19. cmd.Parameters.AddWithValue("@Content", bytes);
  20. con.Open();
  21. cmd.ExecuteNonQuery();
  22. con.Close();
  23. }
  24. }
  25. }
  26. }
  27. Response.Redirect(Request.Url.AbsoluteUri);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement