473,320 Members | 1,838 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,320 software developers and data experts.

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

1
Hi ....

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

I'm Trying to upload & Download .pptx,.docx,.pdf,.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.PostedFile.ContentType" 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 "MySQlCommandLine Client" If so how???

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

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

Similar topics

11
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
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...
9
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 =...
2
by: zalina | last post by:
hai..does anyone have example how to connect to mysql database using c++ language?
3
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...
1
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
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
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
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...
1
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?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.