Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. public partial class IndexForm : System.Web.UI.Page
  2. {
  3.  
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. if (!IsPostBack)
  7. {
  8. BindGrid();
  9. }
  10.  
  11. }
  12. private void BindGrid()
  13. {
  14. string CurrentUser = User.Identity.Name;
  15. string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  16. using (SqlConnection con = new SqlConnection(constr))
  17. {
  18.  
  19. if (CurrentUser == null || CurrentUser == "" || CurrentUser == " ")
  20. {
  21. //Zaprzeczenie w if'ie nie działa...
  22. }
  23. else if (CurrentUser != null || CurrentUser != "" || CurrentUser != " ")
  24. {
  25. using (SqlCommand cmd = new SqlCommand())
  26. {
  27. GridView GridView1 = LoginView3.FindControl("GridView1") as GridView;
  28. cmd.CommandText = "select Id, Name from tblFiles WHERE email = @CurrentUser";
  29. cmd.Parameters.Add("@CurrentUser", SqlDbType.NVarChar);
  30. cmd.Parameters["@CurrentUser"].Value = User.Identity.Name;
  31. cmd.Connection = con;
  32. con.Open();
  33. GridView1.DataSource = cmd.ExecuteReader();
  34. GridView1.DataBind();
  35. con.Close();
  36.  
  37. }
  38. }
  39.  
  40. }
  41. }
  42.  
  43. protected void DownloadFile(object sender, EventArgs e)
  44. {
  45. int id = int.Parse((sender as LinkButton).CommandArgument);
  46. byte[] bytes;
  47. string fileName, contentType;
  48. string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  49. using (SqlConnection con = new SqlConnection(constr))
  50. {
  51. using (SqlCommand cmd = new SqlCommand())
  52. {
  53. cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
  54. cmd.Parameters.AddWithValue("@Id", id);
  55. cmd.Connection = con;
  56. con.Open();
  57. using (SqlDataReader sdr = cmd.ExecuteReader())
  58. {
  59. sdr.Read();
  60. bytes = (byte[])sdr["Data"];
  61. contentType = sdr["ContentType"].ToString();
  62. fileName = sdr["Name"].ToString();
  63. }
  64. con.Close();
  65. }
  66. }
  67. Response.Clear();
  68. Response.Buffer = true;
  69. Response.Charset = "";
  70. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  71. Response.ContentType = contentType;
  72. Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
  73. Response.BinaryWrite(bytes);
  74. Response.Flush();
  75. Response.End();
  76. }
  77.  
  78. protected void Upload(object sender, EventArgs e)
  79. {
  80. FileUpload FileUpload1 = LoginView3.FindControl("FileUpload1") as FileUpload;
  81. string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
  82. string contentType = FileUpload1.PostedFile.ContentType;
  83. string email = User.Identity.Name;
  84.  
  85. using (Stream fs = FileUpload1.PostedFile.InputStream)
  86. {
  87. using (BinaryReader br = new BinaryReader(fs))
  88. {
  89. byte[] bytes = br.ReadBytes((Int32)fs.Length);
  90. string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  91. using (SqlConnection con = new SqlConnection(constr))
  92. {
  93. string query = "insert into tblFiles values (@Name, @ContentType, @Data, @email)";
  94. using (SqlCommand cmd = new SqlCommand(query))
  95. {
  96. cmd.Connection = con;
  97. cmd.Parameters.AddWithValue("@Name", filename);
  98. cmd.Parameters.AddWithValue("@ContentType", contentType);
  99. cmd.Parameters.AddWithValue("@Data", bytes);
  100. cmd.Parameters.AddWithValue("@email", email);
  101. con.Open();
  102. cmd.ExecuteNonQuery();
  103. con.Close();
  104. }
  105. }
  106. }
  107. }
  108. Response.Redirect(Request.Url.AbsoluteUri);
  109. }
  110.  
  111. protected void LoginView1_ViewChanged(object sender, EventArgs e)
  112. {
  113.  
  114. }
  115.  
  116. protected void LoginView3_ViewChanged(object sender, EventArgs e)
  117. {
  118.  
  119. }
  120. }
  121.  
  122. CREATE TABLE [dbo].[tblFiles] (
  123. [id] INT IDENTITY (1, 1) NOT NULL,
  124. [Name] VARCHAR (50) NOT NULL,
  125. [ContentType] NVARCHAR (200) NOT NULL,
  126. [Data] VARBINARY (MAX) NOT NULL,
  127. [email] VARCHAR (50) NOT NULL
  128. );
  129.  
  130. foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Files")))
  131.  
  132. public FileContentResult GetFile(int id)
  133. {
  134. SqlDataReader rdr;
  135. byte[] fileContent = null;
  136. string mimeType = "";
  137. string fileName = "";
  138. const string connect = @"Server=your_servername;Database=your_database;User
  139. Id=user_id;password=user_password;";
  140.  
  141. using (var conn = new SqlConnection(connect))
  142. {
  143. var qry = "SELECT FileContent, MimeType, FileName FROM FileStore WHERE ID
  144. = @ID";
  145. var cmd = new SqlCommand(qry, conn);
  146. cmd.Parameters.AddWithValue("@ID", id);
  147. conn.Open();
  148. rdr = cmd.ExecuteReader();
  149. if (rdr.HasRows)
  150. {
  151. rdr.Read();
  152. fileContent = (byte[])rdr["FileContent"];
  153. mimeType = rdr["MimeType"].ToString();
  154. fileName = rdr["FileName"].ToString();
  155. }
  156. }
  157. return File(fileContent, mimeType, fileName);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement