473,586 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Download and Upload ppts, docx, pdf into MySql Database using Asp.Net

1 New Member
Hi ....

I'm a newbie to Asp.Net and MySql.

I'm Trying to upload & Download .pptx,.docx,.pd f,.txt files into MySql Database.

They are uploading successfully but when I'm trying to download those files they are downloading as files only but not with the specified type.

When I'm using "Fileupload.Pos tedFile.Content Type" It is only accepting .txt files but no .docx and .pdf files.

Want to download the file as it was when I had uploaded into data base.

Could anybody please help me out???

Here is my code for uploading a file.

Expand|Select|Wrap|Line Numbers
  1.  protected void btn4_click(object sender, EventArgs e)
  2.         {
  3.             string filePath = uploadimage1.PostedFile.FileName;
  4.  
  5.             string filename =  Path.GetFileName(filePath);
  6.  
  7.             string ext = Path.GetExtension(filename);
  8.  
  9.             string contenttype = String.Empty;
  10.  
  11.  
  12.  
  13.             //Set the contenttype based on File Extension
  14.  
  15.             switch (ext)
  16.             {
  17.  
  18.                 case ".doc":
  19.  
  20.                     contenttype = "application/msword";
  21.  
  22.                     break;
  23.  
  24.                 case ".docx":
  25.  
  26.                     contenttype = "application/msword";
  27.  
  28.                     break;
  29.  
  30.                 case ".txt":
  31.  
  32.                     contenttype = "text/plain";
  33.  
  34.                     break;
  35.  
  36.                 case ".pdf":
  37.  
  38.                     contenttype = "application/pdf";
  39.  
  40.                     break;
  41.  
  42.                 case ".rtf":
  43.  
  44.                     contenttype = "application/rtf";
  45.  
  46.                     break;
  47.                 case ".pptx":
  48.  
  49.                     contenttype = "application/ppt";
  50.  
  51.                     break;
  52.  
  53.  
  54.  
  55.  
  56.             }
  57.  
  58.             if (contenttype != String.Empty)
  59.             {
  60.  
  61.  
  62.  
  63.                 Stream fs = uploadimage1.PostedFile.InputStream;
  64.  
  65.                 BinaryReader br = new BinaryReader(fs);
  66.  
  67.                 Byte[] bytes = br.ReadBytes((Int32)fs.Length);
  68.  
  69.  
  70.  
  71.                 //insert the file into database
  72. //intro is name of the file
  73. // introdoc is document(longblob)
  74.  
  75.                 string strQuery = "insert into introduction(intro, introdoc,type)"+" values 
  76.  
  77. (@Name,@introdoc,@ContenType)";
  78.  
  79.                 MySqlCommand cmd4 = new MySqlCommand(strQuery);
  80.  
  81.                 cmd4.Parameters.Add("@Name", MySqlDbType.VarChar).Value = txtname1.Text;
  82.                 cmd4.Parameters.Add("@introdoc", MySqlDbType.LongBlob).Value = bytes;
  83.                 cmd4.Parameters.Add("@ContenType", MySqlDbType.VarChar).Value= contenttype;
  84.  
  85.  
  86.                 InsertUpdateData4(cmd4);
  87.                 Server.ScriptTimeout = 12000;
  88.                 BindData4();
  89.  
  90.             }
  91.  
  92.             else
  93.             {
  94.                 ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please upload 
  95.  
  96. only doc/docs/pdf/txt/ppt files')", true);
  97.  
  98.             }
  99.  
  100.  
  101.  
  102.             gridview4.Visible = true;
  103.             lblnewrecord.Visible = true;
  104.             lblname1.Visible = true;
  105.             txtname1.Visible = true;
  106.             lblimage1.Visible = true;
  107.             uploadimage1.Visible = true;
  108.             btnadd4.Visible = true;
  109.  
  110.  
  111.  
  112.         }
Here Is my Code for Downloading
Expand|Select|Wrap|Line Numbers
  1.  protected void indexchangedprocdoc(object sender, EventArgs e)
  2.         {
  3.             con.Open();
  4.             MySqlCommand cmd = new MySqlCommand("select id,proc,procdoc,type from proceduredocs 
  5.  
  6. where id=@id", con);
  7.             cmd.Parameters.AddWithValue("id", gvpdoc.SelectedRow.Cells[0].Text);
  8.             MySqlDataReader dr = cmd.ExecuteReader();
  9.  
  10.  
  11.             if (dr.Read())
  12.             {
  13.                 Response.Clear();
  14.                 Response.Buffer = true;
  15.                  Response.ContentType = dr["type"].ToString();
  16.                 // to open file prompt Box open or Save file
  17.                 Response.AddHeader("content-disposition", "attachment;filename=" + dr
  18.  
  19. ["intro"].ToString());
  20.  
  21.                 Response.Charset = "";
  22.                 Response.Cache.SetCacheability(HttpCacheability.NoCache);
  23.                 Response.BinaryWrite((byte[])dr["introdoc"]);
  24.                 Response.End();
  25.  
  26.             }
  27.             else
  28.             {
  29.                 ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Some Thing gone 
  30.  
  31. wrong')", true);
  32.  
  33.             }
  34.  
  35.     }
  36.  
=============== ====

Please help me out.

Also When reading file name using DataReader it is reading the file name until the first space in the string please give me solution for this also.

If any Mysql experts are here please give me solutions for these also:

*** Is it possible to upload files and image from "MySQlCommandLi ne Client" If so how???

*** How take back up of a database using "MySQlCommandLi ne Client" command prompt
Jan 28 '13 #1
0 2276

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

Similar topics

11
5223
by: menmysql | last post by:
i am getting the following error while connecting mysql database using jsp java.sql.SQLException: Communication link failure: Bad handshake can any one tell what is the actual problem
8
4631
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and displaying records. now i wrote all the JDBC code in .java and i am accessing that code in jsp file. but this time i am getting only exceptions not...
9
3175
by: christopher_board | last post by:
Hi all. I am trying to connect to a MySQL Database using PHP on a local machine using locahost. I am using the following code <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
2
2229
by: zalina | last post by:
hai..does anyone have example how to connect to mysql database using c++ language?
3
5219
by: Suresh P | last post by:
Hi All, I tried to access the mysql database in ODBC using ip address and username/password. It returns, "cannot connect to MySQL server on IP ADDRESS(10060)". This could be related to Firewall on the server. Is there anyway to access MySQL database using ODBC without opening port 3306?
1
1908
kwaxy
by: kwaxy | last post by:
Hey Frendz, I can upload files into MS Access database, but unable to upload files to MSSQL database using ASP, can anyone help me plz ????
1
2535
by: berry | last post by:
Hi all, I am scratching my head on how to store image that user upload to the MySQL database in vb6? Please HELP me!! Thank you very much..
2
2058
by: maheshonmail | last post by:
i want to store pictures in mysql database using php code/ so please send me the most efficient code for doing this.
1
6731
by: nadeenahmed | last post by:
I have Connected to a MySQL Database using a neatbeans editor. Now, I want to use that same database I created earlier on another pc. Can anyone help and tell me how that is done, please? Thank you.
1
12128
by: santhanalakshmi | last post by:
Hi, I wrote some coding, to import excel data in mysql database using PHP Script? But my script, its not all inserting the excel data in mysql database. I don't know, what mistake did i made? Please help me out <?php $strPath = realpath(basename(getenv($_SERVER))); echo $strPath; $excel_app = new COM("Excel.application") or Die...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.