Click here to Skip to main content
15,890,717 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 and also i take one folder name Upload in my website

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 path of file will be stored in data base.and physical file will be stored in Upload folder

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

for that

i have taken another webfrom on that website that web form contain one dropdownlist for select file what u want to download
and also bind with database with dropdownlist 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

when i selcted the dropdownlist item[1] then the item[0] only downloaded
so what i can do

send me u suggestion

Regrds
Reshma
Posted
Updated 2-Nov-11 21:40pm
v2

1 solution

OT - Your stored file path logic is not very good - what happens if someone else uploads a file with the same name, you will overwrite the existing file and make the original record point to an incorrect file.

As a minimum I would got for some logic that involves using either the DateTime or a Guid, if required you can add an extra column to your database that contains the original filename for display purposes.
 
Share this answer
 
Comments
Reshma89 3-Nov-11 4:27am    
ya i can take it filename the filepath will be stored, In storing there is no problem it can stored and it can be bound with dropdownlist it shows the filename
but when i can download file from selected item of the dropdown list it can't be downloaded
Reiss 3-Nov-11 4:47am    
My comment was not that what you have written for storing the file doesn't work (hence the OT - Off Topic), rather that it doesn't scale.

If person A uploads a file called MyFile.txt and then person B then uploads a a file also called MyFile.txt, the first file on the server will be over-written and you will have two database records pointing to the same physical file.

Depending on your actual requirements you should implement some logic that ensures the filenames on the server are unique or that a second record isn't created
Reshma89 3-Nov-11 5:57am    
yes i have implemented the logic whether it exist then send me report the file was already uploaded in to database select another file but when i download the file from dropdownlist the item[0] will be uploaded only remaining items of the dropdownlist can't be downloaded

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