你好,我想通过下拉列表从数据库下载文件,这些文件以前已上传到服务器(数据库) [英] hello i want to download a file from database through dropdownlist what have peviously upload into sever(databse)
问题描述
你好
我想下载以前上传的文件.
我有一个包含以下内容的asp.net应用程序.
一个ID文本框和一个用于上载文档的文件上载控件,以及一个用于上载文档的按钮,我也使用一个文件夹名称在我的网站中上载
我创建了一个数据库
EID整数主键
EFilename nvarchar(100)
然后,我编写了用于在button_click中上传文件的编码.
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.
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();
然后,文件的路径将存储在数据库中,而物理文件将存储在Upload文件夹中
现在我想下载一个我上传到数据库的文件.
为此
我从该网站上获取了另一个Web站点,该Web表单包含一个下拉列表,用于选择要下载的文件
并通过dropdownlist与数据库绑定,我也可以通过一个按钮进行下载
我在button_click中编写了以下代码以下载
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
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();
}
它仅适用于dropdpwnlist item [0]元素,只能下载
但不适用于下拉列表中的其余项目
当我选择下拉列表项目[1]时,项目[0]仅下载
所以我能做的
给我你的建议
问候
Reshma
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
推荐答案
OT-您存储的文件路径逻辑不是很好-如果其他人上传具有相同名称的文件,您将覆盖现有文件并将原始记录指向错误的文件.
至少我会得到一些涉及使用DateTime
或Guid
的逻辑的信息,如果需要,您可以在数据库中添加一个额外的列,其中包含用于显示目的的原始文件名.
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 theDateTime
or aGuid
, if required you can add an extra column to your database that contains the original filename for display purposes.
这篇关于你好,我想通过下拉列表从数据库下载文件,这些文件以前已上传到服务器(数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!