Exemplo n.º 1
0
        private void PopulateUploadedFiles()
        {
            string           strcon   = ConfigurationManager.ConnectionStrings["Lab3"].ConnectionString;
            SqlConnection    con      = new SqlConnection(strcon);
            String           sqlQuery = "SELECT * from [File]";
            List <Lab1.File> allFiles = new List <Lab1.File>();

            con.Open();
            using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Lab1.File newFile = new Lab1.File()
                        {
                            FileID        = Convert.ToInt32(reader["FileID"]),
                            FileName      = reader["FileName"].ToString(),
                            FileSize      = Convert.ToInt32(reader["FileSize"]),
                            ContentType   = reader["ContentType"].ToString(),
                            FileExtension = reader["FileExtension"].ToString(),
                            FileContent   = Encoding.ASCII.GetBytes(reader["FileContent"].ToString())
                        };
                        allFiles.Add(newFile);
                    }
                }
                FileList.DataSource = allFiles;
                FileList.DataBind();
            }
        }
Exemplo n.º 2
0
        protected void FileList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                int fileID = Convert.ToInt32(e.CommandArgument);

                string        strcon   = ConfigurationManager.ConnectionStrings["Lab3"].ConnectionString;
                SqlConnection con      = new SqlConnection(strcon);
                String        sqlQuery = "SELECT * from [File] WHERE FileID =" + fileID;

                con.Open();
                using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
                {
                    Lab1.File newFile = new Lab1.File();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            newFile.FileID        = Convert.ToInt32(reader["FileID"]);
                            newFile.FileName      = reader["FileName"].ToString();
                            newFile.FileSize      = Convert.ToInt32(reader["FileSize"]);
                            newFile.ContentType   = reader["ContentType"].ToString();
                            newFile.FileExtension = reader["FileExtension"].ToString();
                            newFile.FileContent   = (byte[])reader["FileContent"];
                        }

                        if (newFile.FileID > 0)
                        {
                            byte[] fileData = newFile.FileContent;
                            Response.AddHeader("Content-type", newFile.ContentType);
                            Response.AddHeader("Content-Disposition", "attachment; filename=" + newFile.FileName);

                            byte[] dataBlock = new byte[0x1000];
                            long   fileSize;
                            int    bytesRead;
                            long   totalsBytesRead = 0;

                            using (Stream st = new MemoryStream(fileData))
                            {
                                fileSize = st.Length;
                                while (totalsBytesRead < fileSize)
                                {
                                    if (Response.IsClientConnected)
                                    {
                                        bytesRead = st.Read(dataBlock, 0, dataBlock.Length);
                                        Response.OutputStream.Write(dataBlock, 0, bytesRead);

                                        Response.Flush();
                                        totalsBytesRead += bytesRead;
                                    }
                                }
                            }
                            Response.End();
                        }
                    }
                }
            }
        }