Click here to Skip to main content
11,641,659 members (60,975 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MySQL
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
Posted 28-Jan-13 2:27am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 310
1 DamithSL 213
2 Afzaal Ahmad Zeeshan 139
3 CPallini 134
4 OriginalGriff 120
0 Mika Wendelius 460
1 OriginalGriff 215
2 DamithSL 213
3 CPallini 169
4 Afzaal Ahmad Zeeshan 129


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100