Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I want to download a file what i have previously uploaded.

I have an asp.net application that contain.

One textbox for id and one fileupload control for upload document and one button for upload document.

I am created a database
Eid integer primary key
EFilename nvarchar(100)

Then i write coding for upload the file in button_click.
C#
using System.Data.SqlClient
 SqlConnection con;
        con = new SqlConnection("Server=DELL\\SQLEXPRESS;database=Student;integrated security=true");
        con.Open();
        string ph = "~/Upload/" + FileUpload1.FileName;
        FileUpload1.SaveAs(MapPath(ph));
        string str = "insert into upload values('" + TextBox1.Text + "','" + ph + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        Response.Write("Value inserted");
        con.Close();

Then the value will be stored in data base.

Now i want download a file what i have uploaded into database.

Now i take another web form that contain one dropdownlist i bind dropdownlist with database (Efilename column of already eisting table in database)
and take one button for download
and write the following code.
C#
string fname = DropDownList1.SelectedItem.Text;
string filepath = Server.MapPath(".\\Uploads\\" + fname);
FileInfo fi = new FileInfo(filepath);
if (fi.Exists)
{   Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fname);
        Response.AddHeader("Content-Length", fi.Length.ToString());
        Response.ContentType = "application";
        Response.TransmitFile(fi.FullName);
        Response.End();
    }

When i execute there no result.

Give u R Answer.
Posted
Updated 1-Nov-11 22:19pm
v2

The path seems doubtful.
You are saving file in upload folder ("~/Upload/" + FileUpload1.FileName) and trying to get that file from uploads folder (Server.MapPath(".\\Uploads\\" + fname)). It won't work definitely.
 
Share this answer
 
v2
Hi prerak patel
i take one folder that is Upload in that website

what i upload the file filepath will be stored in database and physical file will be stored in that Upload folder in that website
 
Share this answer
 
Hi
i have taken one dropdownlist for select file what u want to download
and also bind with database and i have take one button also for downloading

i have write following a code in button_click for downloading
C#
string fname = DropDownList1.SelectedItem.Text;
    string filepath = Server.MapPath(@"~\Upload\" + fname);
    FileInfo fi = new FileInfo(filepath);
    if (fi.Exists )
    {
        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fname);
        Response.AddHeader("Content-Length", fi.Length.ToString());
        Response.ContentType = "application";
        Response.TransmitFile(fi.FullName);
        Response.End();
    }

it's working only for dropdpwnlist item[0] element can only downloaded


but not working for remaining items in dropdownlist
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900