Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 27, 2012 03:53 AM by Mamba Dai - MSFT
Participant
1142 Points
397 Posts
Jul 12, 2012 07:14 AM|LINK
Hi all,
In my application(ASP.net/ActiveXcontrol/MSSQL VS-2005) i want to
1. upload byte[] data from client system (D:\Temp\MyFile\File.File) to server database(Ms SQL)
2. Download Byte[] data from Server database to client machine(D:Temp\MyFile\File.File).
without using Fileupload control.
I have to upload Multiple Files (D:Temp\MyFile\ ( Resume.doc and Photo.bmp and Markscard Scan copies in the same folder.)
With File access permissons.
All-Star
23531 Points
2683 Posts
Microsoft
Jul 18, 2012 05:14 AM|LINK
Hi,
In accorance with your description, in a word, you want to provide the functionality uploading and download files from database. To upload multiple files you can check this solved thread or by search engine find the way:
http://forums.asp.net/t/1332463.aspx
murthysrn 1. upload byte[] data from client system (D:\Temp\MyFile\File.File) to server database(Ms SQL)
To save the files into databse, you need to set the type of column to "varbinary".
Now I want to supply a sample code to demonstrate how to upload and download files: Note: for demnstrated purpose, I juse use fileupload control:
Key code snippet for uploading:
byte[] byteArray = null; string filename=null; if (FileUpload1.HasFile) { using (FileStream fs = new FileStream(FileUpload1.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { byteArray = new byte[fs.Length]; filename=FileUpload1.FileName; int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length); } } using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString())) { connection.Open(); using (SqlCommand cmd = new SqlCommand("INSERT INTO pdfTable1 (pdfName, pdfContent) VALUES (@pdfName, @pdfContent)", connection)) { cmd.Parameters.Add("@pdfName",SqlDbType.VarChar).Value = filename; cmd.Parameters.Add("@pdfContent", SqlDbType.VarBinary).Value = byteArray; cmd.ExecuteNonQuery(); } connection.Close(); }
I use GirdView to display the fils in the database, and each row puts a downloading button let user click to downlaod file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="pdfName" onrowcommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Click to DownLoad"> <ItemTemplate> <asp:Button ID="btnDownload" runat="server" Text='<%#Eval("pdfName") %>' CommandName='<%#Eval("pdfName") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDataBaseConnectionString %>" SelectCommand="SELECT [pdfName] FROM [pdfTable1]"></asp:SqlDataSource> <br />
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { this.ClientScript.RegisterClientScriptBlock(this.GetType(), "redirect", "alert('fdada');window.location=\"Default.aspx\";", true); string fileName = e.CommandName; byte[] buffer = null; using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString())) { connection.Open(); using (SqlCommand command = new SqlCommand("select pdfContent from pdfTable1", connection)) { buffer = (byte[])command.ExecuteScalar(); } connection.Close(); } long fileSize = (long)buffer.Length; Response.Clear(); Response.ContentType = "application/oc-stream"; //Response.ContentType = "application/msword"; //you also can write this, if the client install pdf plug-in then the pdf file will be displayed online //Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(fileName)); // Response.AddHeader("Content-Length", fileSize.ToString()); //Response.OutputStream.Write(buffer, 0, (int)buffer.Length); Response.BinaryWrite(buffer); Response.Flush(); Response.Close(); }
Jul 26, 2012 07:43 AM|LINK
File uplod with out using. AJAX or Third party control.
is there any other way ?(http/WebClient objects)
Jul 27, 2012 03:53 AM|LINK
murthysrn File uplod with out using. AJAX or Third party control. is there any other way ?(http/WebClient objects)
You can use the httpwebrequest to upload the data or ftpwebrequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn.microsoft.com/en-us/library/System.Net.FtpWebRequest.aspx
murthysrn
Participant
1142 Points
397 Posts
Upload and Download Byte[] File Data.
Jul 12, 2012 07:14 AM|LINK
Hi all,
In my application(ASP.net/ActiveXcontrol/MSSQL VS-2005) i want to
1. upload byte[] data from client system (D:\Temp\MyFile\File.File) to server database(Ms SQL)
2. Download Byte[] data from Server database to client machine(D:Temp\MyFile\File.File).
without using Fileupload control.
I have to upload Multiple Files (D:Temp\MyFile\ ( Resume.doc and Photo.bmp and Markscard Scan copies in the same folder.)
With File access permissons.
Murthy.
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: Upload and Download Byte[] File Data.
Jul 18, 2012 05:14 AM|LINK
Hi,
In accorance with your description, in a word, you want to provide the functionality uploading and download files from database. To upload multiple files you can check this solved thread or by search engine find the way:
http://forums.asp.net/t/1332463.aspx
To save the files into databse, you need to set the type of column to "varbinary".
Now I want to supply a sample code to demonstrate how to upload and download files:
Note: for demnstrated purpose, I juse use fileupload control:
Key code snippet for uploading:
byte[] byteArray = null; string filename=null; if (FileUpload1.HasFile) { using (FileStream fs = new FileStream(FileUpload1.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { byteArray = new byte[fs.Length]; filename=FileUpload1.FileName; int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length); } } using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString())) { connection.Open(); using (SqlCommand cmd = new SqlCommand("INSERT INTO pdfTable1 (pdfName, pdfContent) VALUES (@pdfName, @pdfContent)", connection)) { cmd.Parameters.Add("@pdfName",SqlDbType.VarChar).Value = filename; cmd.Parameters.Add("@pdfContent", SqlDbType.VarBinary).Value = byteArray; cmd.ExecuteNonQuery(); } connection.Close(); }I use GirdView to display the fils in the database, and each row puts a downloading button let user click to downlaod file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="pdfName" onrowcommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Click to DownLoad"> <ItemTemplate> <asp:Button ID="btnDownload" runat="server" Text='<%#Eval("pdfName") %>' CommandName='<%#Eval("pdfName") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDataBaseConnectionString %>" SelectCommand="SELECT [pdfName] FROM [pdfTable1]"></asp:SqlDataSource> <br />protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { this.ClientScript.RegisterClientScriptBlock(this.GetType(), "redirect", "alert('fdada');window.location=\"Default.aspx\";", true); string fileName = e.CommandName; byte[] buffer = null; using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString())) { connection.Open(); using (SqlCommand command = new SqlCommand("select pdfContent from pdfTable1", connection)) { buffer = (byte[])command.ExecuteScalar(); } connection.Close(); } long fileSize = (long)buffer.Length; Response.Clear(); Response.ContentType = "application/oc-stream"; //Response.ContentType = "application/msword"; //you also can write this, if the client install pdf plug-in then the pdf file will be displayed online //Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(fileName)); // Response.AddHeader("Content-Length", fileSize.ToString()); //Response.OutputStream.Write(buffer, 0, (int)buffer.Length); Response.BinaryWrite(buffer); Response.Flush(); Response.Close(); }Feedback to us
Develop and promote your apps in Windows Store
murthysrn
Participant
1142 Points
397 Posts
Re: Upload and Download Byte[] File Data.
Jul 26, 2012 07:43 AM|LINK
File uplod with out using. AJAX or Third party control.
is there any other way ?(http/WebClient objects)
Murthy.
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: Upload and Download Byte[] File Data.
Jul 27, 2012 03:53 AM|LINK
Hi,
You can use the httpwebrequest to upload the data or ftpwebrequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn.microsoft.com/en-us/library/System.Net.FtpWebRequest.aspx
Feedback to us
Develop and promote your apps in Windows Store