참고 하세요~
<!-- http://www.codeproject.com/aspnet/upsanddowns.asp 에서 다운 받은 것 입니다. -->
<%@ Page Language="c#" Trace="false" Debug="true" %>
<%@ Register TagPrefix="oda" Namespace="Oracle.DataAccess" Assembly="Oracle.DataAccess, Version=9.2.0.11, Culture=neutral, PublicKeyToken=89b483f429c47342" %>
<%@ Register TagPrefix="ic" Namespace="ICSharpCode.SharpZipLib" Assembly="ICSharpCode.SharpZipLib, Version=0.5.0.0, Culture=neutral, PublicKeyToken=1b03e6acf1164f73" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Oracle.DataAccess.Types" %>
<%@ Import Namespace="Oracle.DataAccess.Client" %>
<%@ Import Namespace="ICSharpCode.SharpZipLib.Zip" %>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
if (!Page.IsPostBack) {
// Open our database connection to the table with the BLOB field and fill our datagrid
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
BindDataGrid(oConn, dgdFiles, "SELECT DOC_ID AS FILE_ID, FILENAME AS FILE_NAME, DBMS_LOB.GETLENGTH(DOCUMENT) AS FILE_SIZE FROM DOCUMENT FILES");
}
}
private void btnUpload_Click(object sender, System.EventArgs e) {
// If no file was uploaded then you probably want to inform the user to select a file
// before clicking the upload button.
if (filUpload.PostedFile != null) {
// Get a reference to the uploaded file
HttpPostedFile filPosted = filUpload.PostedFile;
int intFileLength = System.Convert.ToInt32(filPosted.ContentLength);
// Ensure that there were contents to the file. Exit out of the function if there is no content.
if (intFileLength == 0) {
return;
}
// Open our database connection to the table with the BLOB field
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
OracleDataAdapter oDA = new OracleDataAdapter("SELECT * FROM FILES", oConn);
OracleCommandBuilder oCB = new OracleCommandBuilder(oDA);
DataSet ds = new DataSet();
DataRow dr;
// Fill the dataset with the schema but not the data
oDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
oConn.Open();
oDA.FillSchema(ds, SchemaType.Source, "FILES");
// This may not be necessary
byte[] byteData = new byte[intFileLength];
filPosted.InputStream.Read(byteData, 0, intFileLength);
// Create a new row to insert into the table
dr = ds.Tables["FILES"].NewRow();
dr["FILE_ID"] = 1; // this should not be needed ... it's a sequence but will not allow nulls? so the 1 is a placeholder
dr["FILE_NAME"] = System.IO.Path.GetFileName(filPosted.FileName);
dr["FILE_DATA"] = byteData;
// Append the new row to the dataset
ds.Tables["FILES"].Rows.Add(dr);
// Update the table using our dataset against our data adapter
oDA.Update(ds, "FILES");
// Close our connection
oConn.Close();
// Refresh the contents to the datagrid
BindDataGrid(oConn, dgdFiles, "SELECT DOC_ID AS FILE_ID, FILENAME AS FILE_NAME, DBMS_LOB.GETLENGTH(DOCUMENT) AS FILE_SIZE FROM DOCUMENT FILES");
}
}
private void btnDownload_Click(object sender, System.EventArgs e) {
// A temp object to be used in iterating through the datagrid
CheckBox chkControl;
// will hold a comma seperated list of the IDs that were selected
string strIDs = "";
// Find the checked items in the datagrid and append them to the string
foreach (DataGridItem dgi in dgdFiles.Items) {
chkControl = (CheckBox)dgi.FindControl("chkSelection");
if (chkControl.Checked) {
strIDs += ((Label)dgi.FindControl("lblFileID")).Text.ToString() + ",";
}
}
// If there were files selected then create the ZIP file for download
if (strIDs.Length > 0) {
// Connect to the database and grab the selected documents
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
OracleDataAdapter oDA = new OracleDataAdapter("SELECT FILE_NAME, FILE_DATA, DBMS_LOB.GETLENGTH(FILE_DATA) AS FILE_SIZE FROM FILES WEHRE FILE_ID IN (" + strIDs.Substring(0, strIDs.Length -1) + ")", oConn);
DataSet ds = new DataSet();
// Open the database connection and fill the dataset
oConn.Open();
oDA.Fill(ds, "FILES");
// Create the ZIP file that will be downloaded. Need to name the file something unique ...
string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("./") + strNow + ".zip"));
zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression
// Loop through the dataset to fill the zip file
foreach (DataRow dr in ds.Tables["FILES"].Rows) {
ZipEntry zipEntry = new ZipEntry(dr["FILE_NAME"].ToString());
zipOS.PutNextEntry(zipEntry);
zipOS.Write((byte[])dr["FILE_DATA"], 0, System.Convert.ToInt32(dr["FILE_SIZE"]));
}
// Close the database connection
oConn.Close();
// Compress and close the zip file
zipOS.Finish();
zipOS.Close();
// When the page reloads we need to redirect the user to the file
string script1 = "<script language=javascript>function gotoZip() {document.location.href = '" + strNow + ".zip';}</" + "script>";
string script2 = "<script language=javascript>document.body.onload = gotoZip;</" + "script>";
Page.RegisterStartupScript("download1", script1);
Page.RegisterStartupScript("download2", script2);
}
}
private void lblFileSize_DataBinding(object sender, System.EventArgs e) {
// Formatting the label to show the file size in kilobytes
Label lblSize = (Label)sender;
lblSize.Text = String.Format("{0:#,###}", System.Math.Ceiling(System.Convert.ToDouble(lblSize.Text) / 1024));
}
private void BindDataGrid(OracleConnection o, DataGrid d, string sql) {
OracleCommand oComm = new OracleCommand(sql, o);
o.Open();
d.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection);
d.DataBind();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>An Example of Uploading Files to a Database ... and Downloading in a Single Zip File</title>
<script language="javascript" type="text/javascript">
// a simple function to find toggle all of the checkboxes in a form
function checkAll(o) {
for (var i = 0; i < document.myForm.elements.length; i++) {
if (document.myForm.elements[i].type == "checkbox") {
document.myForm.elements[i].checked = o.checked;
}
}
}
</script>
<style>
fieldset {
cursor: default;
font-family: Arial;
}
div {
width: 400px;
}
</style>
</head>
<body>
<form runat="server" id="myForm" name="myForm" method="post" enctype="multipart/form-data">
<div align="center">
<fieldset>
<legend>Upload Portion</legend>
<input runat="server" id="filUpload" type="file">
<asp:Button runat="server" id="btnUpload" OnClick="btnUpload_Click" Text="Begin Upload" />
</fieldset>
<fieldset>
<legend>Download Portion</legend>
<asp:DataGrid runat="server" id="dgdFiles" AutoGenerateColumns="false">
<columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input runat="server" type="checkbox" ID="chkSelectAll" title="Select All Files" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:Checkbox ID="chkSelection" runat="server" />
<asp:Label ID="lblFileID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_ID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="File Name">
<ItemTemplate>
<asp:Label ID="lblFileName" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_NAME") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="File Size">
<ItemStyle HorizontalAlign="right" />
<ItemTemplate>
<asp:Label ID="lblFileSize" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_SIZE") %>' runat="server" OnDataBinding="lblFileSize_DataBinding" />
<asp:Label ID="lblKB" Text=" kb" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
<asp:Button runat="server" id="btnDownload" OnClick="btnDownload_Click" Text="Zip and Download" />
</fieldset>
</div>
</form>
</body>
</html>
<!-- http://www.codeproject.com/aspnet/upsanddowns.asp 에서 다운 받은 것 입니다. -->
<%@ Page Language="c#" Trace="false" Debug="true" %>
<%@ Register TagPrefix="oda" Namespace="Oracle.DataAccess" Assembly="Oracle.DataAccess, Version=9.2.0.11, Culture=neutral, PublicKeyToken=89b483f429c47342" %>
<%@ Register TagPrefix="ic" Namespace="ICSharpCode.SharpZipLib" Assembly="ICSharpCode.SharpZipLib, Version=0.5.0.0, Culture=neutral, PublicKeyToken=1b03e6acf1164f73" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Oracle.DataAccess.Types" %>
<%@ Import Namespace="Oracle.DataAccess.Client" %>
<%@ Import Namespace="ICSharpCode.SharpZipLib.Zip" %>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
if (!Page.IsPostBack) {
// Open our database connection to the table with the BLOB field and fill our datagrid
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
BindDataGrid(oConn, dgdFiles, "SELECT DOC_ID AS FILE_ID, FILENAME AS FILE_NAME, DBMS_LOB.GETLENGTH(DOCUMENT) AS FILE_SIZE FROM DOCUMENT FILES");
}
}
private void btnUpload_Click(object sender, System.EventArgs e) {
// If no file was uploaded then you probably want to inform the user to select a file
// before clicking the upload button.
if (filUpload.PostedFile != null) {
// Get a reference to the uploaded file
HttpPostedFile filPosted = filUpload.PostedFile;
int intFileLength = System.Convert.ToInt32(filPosted.ContentLength);
// Ensure that there were contents to the file. Exit out of the function if there is no content.
if (intFileLength == 0) {
return;
}
// Open our database connection to the table with the BLOB field
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
OracleDataAdapter oDA = new OracleDataAdapter("SELECT * FROM FILES", oConn);
OracleCommandBuilder oCB = new OracleCommandBuilder(oDA);
DataSet ds = new DataSet();
DataRow dr;
// Fill the dataset with the schema but not the data
oDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
oConn.Open();
oDA.FillSchema(ds, SchemaType.Source, "FILES");
// This may not be necessary
byte[] byteData = new byte[intFileLength];
filPosted.InputStream.Read(byteData, 0, intFileLength);
// Create a new row to insert into the table
dr = ds.Tables["FILES"].NewRow();
dr["FILE_ID"] = 1; // this should not be needed ... it's a sequence but will not allow nulls? so the 1 is a placeholder
dr["FILE_NAME"] = System.IO.Path.GetFileName(filPosted.FileName);
dr["FILE_DATA"] = byteData;
// Append the new row to the dataset
ds.Tables["FILES"].Rows.Add(dr);
// Update the table using our dataset against our data adapter
oDA.Update(ds, "FILES");
// Close our connection
oConn.Close();
// Refresh the contents to the datagrid
BindDataGrid(oConn, dgdFiles, "SELECT DOC_ID AS FILE_ID, FILENAME AS FILE_NAME, DBMS_LOB.GETLENGTH(DOCUMENT) AS FILE_SIZE FROM DOCUMENT FILES");
}
}
private void btnDownload_Click(object sender, System.EventArgs e) {
// A temp object to be used in iterating through the datagrid
CheckBox chkControl;
// will hold a comma seperated list of the IDs that were selected
string strIDs = "";
// Find the checked items in the datagrid and append them to the string
foreach (DataGridItem dgi in dgdFiles.Items) {
chkControl = (CheckBox)dgi.FindControl("chkSelection");
if (chkControl.Checked) {
strIDs += ((Label)dgi.FindControl("lblFileID")).Text.ToString() + ",";
}
}
// If there were files selected then create the ZIP file for download
if (strIDs.Length > 0) {
// Connect to the database and grab the selected documents
OracleConnection oConn = new OracleConnection("Data Source=MyDatabase;User ID=MyUser;Password=MyPassword");
OracleDataAdapter oDA = new OracleDataAdapter("SELECT FILE_NAME, FILE_DATA, DBMS_LOB.GETLENGTH(FILE_DATA) AS FILE_SIZE FROM FILES WEHRE FILE_ID IN (" + strIDs.Substring(0, strIDs.Length -1) + ")", oConn);
DataSet ds = new DataSet();
// Open the database connection and fill the dataset
oConn.Open();
oDA.Fill(ds, "FILES");
// Create the ZIP file that will be downloaded. Need to name the file something unique ...
string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("./") + strNow + ".zip"));
zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression
// Loop through the dataset to fill the zip file
foreach (DataRow dr in ds.Tables["FILES"].Rows) {
ZipEntry zipEntry = new ZipEntry(dr["FILE_NAME"].ToString());
zipOS.PutNextEntry(zipEntry);
zipOS.Write((byte[])dr["FILE_DATA"], 0, System.Convert.ToInt32(dr["FILE_SIZE"]));
}
// Close the database connection
oConn.Close();
// Compress and close the zip file
zipOS.Finish();
zipOS.Close();
// When the page reloads we need to redirect the user to the file
string script1 = "<script language=javascript>function gotoZip() {document.location.href = '" + strNow + ".zip';}</" + "script>";
string script2 = "<script language=javascript>document.body.onload = gotoZip;</" + "script>";
Page.RegisterStartupScript("download1", script1);
Page.RegisterStartupScript("download2", script2);
}
}
private void lblFileSize_DataBinding(object sender, System.EventArgs e) {
// Formatting the label to show the file size in kilobytes
Label lblSize = (Label)sender;
lblSize.Text = String.Format("{0:#,###}", System.Math.Ceiling(System.Convert.ToDouble(lblSize.Text) / 1024));
}
private void BindDataGrid(OracleConnection o, DataGrid d, string sql) {
OracleCommand oComm = new OracleCommand(sql, o);
o.Open();
d.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection);
d.DataBind();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>An Example of Uploading Files to a Database ... and Downloading in a Single Zip File</title>
<script language="javascript" type="text/javascript">
// a simple function to find toggle all of the checkboxes in a form
function checkAll(o) {
for (var i = 0; i < document.myForm.elements.length; i++) {
if (document.myForm.elements[i].type == "checkbox") {
document.myForm.elements[i].checked = o.checked;
}
}
}
</script>
<style>
fieldset {
cursor: default;
font-family: Arial;
}
div {
width: 400px;
}
</style>
</head>
<body>
<form runat="server" id="myForm" name="myForm" method="post" enctype="multipart/form-data">
<div align="center">
<fieldset>
<legend>Upload Portion</legend>
<input runat="server" id="filUpload" type="file">
<asp:Button runat="server" id="btnUpload" OnClick="btnUpload_Click" Text="Begin Upload" />
</fieldset>
<fieldset>
<legend>Download Portion</legend>
<asp:DataGrid runat="server" id="dgdFiles" AutoGenerateColumns="false">
<columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input runat="server" type="checkbox" ID="chkSelectAll" title="Select All Files" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:Checkbox ID="chkSelection" runat="server" />
<asp:Label ID="lblFileID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_ID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="File Name">
<ItemTemplate>
<asp:Label ID="lblFileName" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_NAME") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="File Size">
<ItemStyle HorizontalAlign="right" />
<ItemTemplate>
<asp:Label ID="lblFileSize" Text='<%# DataBinder.Eval(Container.DataItem, "FILE_SIZE") %>' runat="server" OnDataBinding="lblFileSize_DataBinding" />
<asp:Label ID="lblKB" Text=" kb" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
<asp:Button runat="server" id="btnDownload" OnClick="btnDownload_Click" Text="Zip and Download" />
</fieldset>
</div>
</form>
</body>
</html>
[100%환급,실무전문]SQL/빅데이터/자바/스프링/웹퍼블리싱/안드… | 12-27 | 2878 | ||
[채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육… | 12-11 | 2098 | ||
53 | [평일100%환급7건]웹퍼블리싱,자바&JSP,안드로이드,C#닷넷,SQL기… | 03-15 | 1955 | |
52 | [주말]C#,ASP.NET마스터 | 01-31 | 2093 | |
51 | [기업100%환급,평일주간]SQL기초에서스키마오브젝트,PLSQL,힌트… | 01-31 | 3063 | |
50 | [주말주간]자바&웹,jQUERY,스프링프레임워크,마이바티스 | 01-31 | 1617 | |
49 | [평일주간/야간,주말주간/야간]Spring,MyBatis,Hibernate개발자… | 01-19 | 1941 | |
48 | [평일주간/야간,주말주간/야간]안드로이드개발자과정(Adnroid 교… | 01-11 | 1808 | |
47 | [평일야간,주말주간야간]JAVA,Network&JSP&Spring,MyBatis,Hiber… | 01-03 | 2414 | |
46 | [100%환급,실무전문]SQL/빅데이터/자바/스프링/웹퍼블리싱/안드… | 12-27 | 2878 | |
45 | [주말야간]개발자를위한PLSQL,SQL튜닝,힌트(토/일) | 12-19 | 2021 | |
44 | [평일주간/야간,주말주간/야간]웹퍼블리싱 마스터(HTML5,CSS3,jQ… | 12-14 | 1988 | |
43 | [채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육… | 12-11 | 2098 | |
42 | [주말야간]JAVA,JSP,Spring,PLSQL,힌트,웹퍼블리싱,안드로이드,… | 12-09 | 1632 | |
41 | [평일야간,주말야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1853 | |
40 | [기업100%환급]오라클&자바웹스프링신입과정3주(SQL,JAVA,JSP,Se… | 12-01 | 2098 | |
39 | [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) | 12-01 | 1505 |
댓글 없음:
댓글 쓰기