473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Uploading & Downloading files in asp.net using C#

Haiii Everbody,

I'm new to this forum, infact this is the first forum i hav ever joined.
I need some help urgently as i'm reaching the deadline of my project.

I created an asp.net website, where administrator will upload files to SQL database which is connected to the webpage. From the web page there will be a download link to download those uploaded files. I have been successful in uploading files to the database. But nothing is striking in my mind regarding how to download files directly from database using asp.net C#.

Please help me in this regard.

Thanks in advacne
Mar 22 '10 #1
11 16603
Anyone....please reply to my post....its very urgent
Mar 22 '10 #2
CroCrew
564 Expert 512MB
Hello raja471,

Glad to help out. But, first we are going to need more information.

Are you storing the files in a BLOB field? Please post the code that you have so far so that we can provide you with the best fit solution to your question/problem.


Happy Coding,
CroCrew~
Mar 24 '10 #3
Thank you for the reply.
The following are the my aspx.cs files for uploading and donwloading files from the database.

I have created a table in sql database which has columns like
Expand|Select|Wrap|Line Numbers
  1. ProductID -- int
  2. Name --- varchar(50)
  3. Type --- varchar(50)
  4. Description --- varchar(max)
  5. Doc --- varbinar(max)
  6.  
UploadFiles.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9.  
  10. public partial class Upload : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.  
  15.     }
  16.     protected void uploading_Click(object sender, EventArgs e)
  17.     {
  18.         if (FileToUpload.PostedFile == null || String.IsNullOrEmpty(FileToUpload.PostedFile.FileName) || FileToUpload.PostedFile.InputStream == null)
  19.         {
  20.             lit_Status.Text = "<br />Error - unable to upload file. Please try again.<br />";
  21.         }
  22.  
  23.         else
  24.         {
  25.             using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ResourcesConnectionString1"].ConnectionString))
  26.             {
  27.                 try
  28.                 {
  29.                     const string SQL = "INSERT INTO Resources (Name, Description, Price, FileType, Doc) VALUES (@FileName, @Description, @Price, @Type, @Doc)";
  30.                     SqlCommand cmd = new SqlCommand(SQL, Conn);
  31.                     cmd.Parameters.AddWithValue("@FileName", TextBox1.Text.Trim());
  32.                     cmd.Parameters.AddWithValue("@Description", TextBox3.Text.Trim());
  33.                     cmd.Parameters.AddWithValue("@Price", TextBox2.Text.Trim());
  34.                     cmd.Parameters.AddWithValue("@Type", FileToUpload.PostedFile.ContentType);
  35.  
  36.                     byte[] imageBytes = new byte[FileToUpload.PostedFile.InputStream.Length + 1];
  37.                     FileToUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
  38.                     cmd.Parameters.AddWithValue("@Doc", imageBytes);
  39.  
  40.                     Conn.Open();
  41.                     cmd.ExecuteNonQuery();
  42.                     lit_Status.Text = "<br />File successfully uploaded - thank you.<br />";
  43.                     Conn.Close();
  44.                 }
  45.                 catch
  46.                 {
  47.                     Conn.Close();
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }

DownloadFiles.aspx.cs

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Data.SqlClient;
  7. using System.Web.UI.WebControls;
  8. using System.Configuration;
  9.  
  10. public partial class delFiles : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.  
  15.     }
  16.     protected void newFile_Click(object sender, EventArgs e)
  17.     {
  18.         Response.Redirect("Upload.aspx");
  19.     }
  20.     protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
  21.     {
  22.         if (e.CommandName == "Donwload")
  23.         {
  24.             String[] Fileinfo = e.CommandArgument.ToString().Split(new char[] { ',' });
  25.  
  26.             int ProductID = Int32.Parse(Fileinfo[0]);
  27.             String Name = Fileinfo[1];
  28.  
  29.             using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ResourcesConnectionString1"].ConnectionString))
  30.             {
  31.                 SqlCommand com = new SqlCommand("select Doc from Resources Where ProductID=@ProductID");
  32.  
  33.                 com.Parameters.AddWithValue("@ProductID", ProductID);
  34.                 com.Connection = con;
  35.                 con.Open();
  36.  
  37.                 byte[] Doc = (Byte[])com.ExecuteScalar();
  38.  
  39.                 Response.AddHeader("Conetent-Disposition", "attachment;filename=" + Name + ".zip");
  40.                 Response.ContentType = "application/octet-stream";
  41.                 Response.BinaryWrite(Doc);
  42.             }
  43.         }
  44.  
  45.  
  46.     }
  47. }
My code is working properly incase of uploading files. it is not responding when i click the download button.

Please help me.....Thanks in advance.
Mar 25 '10 #4
Hey .... I finally got the result....anyway thanks for ur reply....i have one more problem.

I need to create a paypal link to my website....when user clicks the download button, now the file is getting downloaded. Instead of that it download button should direct the page to paypal by taking the "name' and "price" fileds from the gridview. After the amount has been paid by the user in paypal it should redirect to "Save As" window for the corresponding file.

It seems im asking for entire application, but like i said, im a beginner in asp.net. i just started a few days back. I cant provide any code also, because i have not done anuthing in this regard. But help is needed urgently.

Please help me....
Mar 27 '10 #5
Frinavale
9,735 Expert Mod 8TB
What have you tried so far to solve this problem?

You could either have a link on the page that redirects the user to the PayPal page.

Or you could have the server do the Redirect once the user clicks the link (or button).

Or you could place another form on the page that submits to PayPal...


-Frinny
Mar 29 '10 #6
Thank you for the reply.

Like i said before, i have a an upload page where i could upload files with filename and price to a SQL database. I have a datalist for these files in another page. No i want to put a buynow button in each item of the datalist which should take filename and price from the datalist iteself. is this possible for a buynow button to take the fileds from the datalist....if so how....i would be so greatful if u could explain me with an example.

Thank you.
Mar 30 '10 #7
Frinavale
9,735 Expert Mod 8TB
I would use a GridView control or a Repeater for this.

Both of these controls bind to a data source and display it to the user in the way that you specified. With the GridView control you can add a button column to a grid that displays your items. With the Repeater control it's a little more flexible ...but you bind a button (or link) to the data in a similar way.

Check out the these tools and choose according to what you think would be best for your application.

-Frinny
Mar 30 '10 #8
Hai....Thank you for the reply.....It helped me a lot

Now i want to resolve another issue.

I have a table in databae which constists of a varbinary filed and some other fileds. I am dispaying this table in gridview in one page. I added a column in the same table with a button filed for each row. Now if i click that button the varbinary filed in that row of the table should get copied to anothe table which is already there in the database.

Suppose if i click the button in the first row, the varbinary filed in that table should get copied to new table and if a click button in second row....the already added varbinary should get replaced by the varbinary of second row, meaning everytime the data in the new table should get replaced by the button click....

Is there any way to do this....

A detailed explanation would be appreciated.

Thank you
Apr 1 '10 #9
Frinavale
9,735 Expert Mod 8TB
I'm not exactly sure what you're trying to accomplish but it sounds like you already know what you need to do.

When the user clicks the button, get the ID of the row that the user clicked...make a call to the database and retrieve your binary for that row and then make another call to your database which updates the table with the binary data selected/retrieved.

-Frinny
Apr 1 '10 #10
haii....

Thank you for the reply...i solved that problem....now i need help in another issue.

I have a page having some paypal buynow buttons in gridview. When i click a particular buynow button in a particular row...after payment process it should redirect to detailsView of that record. i have created another page having detailsView of the same gridView table. So in returnUrl of paypal buynow button how can i give an url name like "http://www.raja.com/download?ProductID=1". the product id will keep vary according to whch buynow button is clicked.

Thanks in advance.
Apr 5 '10 #11
Frinavale
9,735 Expert Mod 8TB
Raja471,

What is your question?
I don't see a question at all in your last post. You just listed off a bunch of things that you have to do.

-Frinny
Apr 5 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oyvind Ostlund | last post by:
Hello, I have to download a lot of files. And at the moment i am trying with URLLib. But sometimes it doesn't download the whole file. It looks like it stops half way through or something. Is it...
2
by: Dude | last post by:
Somewhere between 9 - 11 megs I am unable to upload, using either aspupload or the built-in uploading functionality. here is the code from web.config <system.web> <httpRuntime...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
4
by: Himanshu | last post by:
hi, Can anybody tell me that thru asp.net using c#, how can we upload and download physical files in any table of SQL Server Database. the uploading part is running successfully but the...
6
by: justsee | last post by:
Hi, I'm using Python 2.3 on Windows for the first time, and am doing something wrong in using urllib to retrieve images from urls embedded in a csv file. If I explicitly specify a url and image...
2
by: prakharv | last post by:
Hi All, Below is the code which I am using to upload a jpeg file to the server. But the problem I am facing is that it is not copying the entire contents of the image file to the webserver and it...
0
by: dixonjm | last post by:
Hi, I have a master page & various pages that will use this master page. Each content page will have a CSS & JS file which will be named the same as the content page. When I try to load the CSS...
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any...
3
by: muziburrehaman | last post by:
i am looking for code in php to upload the 1 gb files. any one can please help me by sending the code....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.