By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this question for a faster answer!
Share on Google+

Post your reply

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