Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void UploadFile(object sender, EventArgs e)
- {
- string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
- string contentType = FileUpload1.PostedFile.ContentType;
- using (Stream fs = FileUpload1.PostedFile.InputStream)
- {
- using (BinaryReader br = new BinaryReader(fs))
- {
- byte[] bytes = br.ReadBytes((Int32)fs.Length);
- string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
- using (MySqlConnection con = new MySqlConnection(constr))
- {
- string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (@FileName, @ContentType, @Content)";
- using (MySqlCommand cmd = new MySqlCommand(query))
- {
- cmd.Connection = con;
- cmd.Parameters.AddWithValue("@FileName", filename);
- cmd.Parameters.AddWithValue("@ContentType", contentType);
- cmd.Parameters.AddWithValue("@Content", bytes);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
- }
- Response.Redirect(Request.Url.AbsoluteUri);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement