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
- protected void btn4_click(object sender, EventArgs e)
- {
- string filePath = uploadimage1.PostedFile.FileName;
- string filename = Path.GetFileName(filePath);
- string ext = Path.GetExtension(filename);
- string contenttype = String.Empty;
- //Set the contenttype based on File Extension
- switch (ext)
- {
- case ".doc":
- contenttype = "application/msword";
- break;
- case ".docx":
- contenttype = "application/msword";
- break;
- case ".txt":
- contenttype = "text/plain";
- break;
- case ".pdf":
- contenttype = "application/pdf";
- break;
- case ".rtf":
- contenttype = "application/rtf";
- break;
- case ".pptx":
- contenttype = "application/ppt";
- break;
- }
- if (contenttype != String.Empty)
- {
- Stream fs = uploadimage1.PostedFile.InputStream;
- BinaryReader br = new BinaryReader(fs);
- Byte[] bytes = br.ReadBytes((Int32)fs.Length);
- //insert the file into database
- //intro is name of the file
- // introdoc is document(longblob)
- string strQuery = "insert into introduction(intro, introdoc,type)"+" values
- (@Name,@introdoc,@ContenType)";
- MySqlCommand cmd4 = new MySqlCommand(strQuery);
- cmd4.Parameters.Add("@Name", MySqlDbType.VarChar).Value = txtname1.Text;
- cmd4.Parameters.Add("@introdoc", MySqlDbType.LongBlob).Value = bytes;
- cmd4.Parameters.Add("@ContenType", MySqlDbType.VarChar).Value= contenttype;
- InsertUpdateData4(cmd4);
- Server.ScriptTimeout = 12000;
- BindData4();
- }
- else
- {
- ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please upload
- only doc/docs/pdf/txt/ppt files')", true);
- }
- gridview4.Visible = true;
- lblnewrecord.Visible = true;
- lblname1.Visible = true;
- txtname1.Visible = true;
- lblimage1.Visible = true;
- uploadimage1.Visible = true;
- btnadd4.Visible = true;
- }
Expand|Select|Wrap|Line Numbers
- protected void indexchangedprocdoc(object sender, EventArgs e)
- {
- con.Open();
- MySqlCommand cmd = new MySqlCommand("select id,proc,procdoc,type from proceduredocs
- where id=@id", con);
- cmd.Parameters.AddWithValue("id", gvpdoc.SelectedRow.Cells[0].Text);
- MySqlDataReader dr = cmd.ExecuteReader();
- if (dr.Read())
- {
- Response.Clear();
- Response.Buffer = true;
- Response.ContentType = dr["type"].ToString();
- // to open file prompt Box open or Save file
- Response.AddHeader("content-disposition", "attachment;filename=" + dr
- ["intro"].ToString());
- Response.Charset = "";
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.BinaryWrite((byte[])dr["introdoc"]);
- Response.End();
- }
- else
- {
- ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Some Thing gone
- wrong')", true);
- }
- }
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