Download Upload ppts,.docx,pdf, into MySql Database
Hiii ....
I'm a newbie to Asp.Net and MySql.
I'm Tryingm 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 ony accepting .txt files but no .docx
amd .pdf files
Want to download the file as it is i had uploaded into data base.
Could anybody please help me out???
Here Is my Code For uploading a File.
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;
}
Here Is my Code for Downloading
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 Whwn 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