2017-08-30 76 views
0

我上傳文件夾中的文件並在ms sql數據庫中保存帶有文件名的路徑。我想根據保存在sql數據庫中的特定記錄的路徑從該文件夾下載這些文件。我搜索了網頁,但得到了保存圖像,文件等數據庫不是文件夾的例子。 如果路徑保存在數據庫中,請幫助如何從文件夾下載文件。ASP.Net從基於文件路徑保存在sql數據庫中的文件夾中下載文件

//Upload files 
protected void btnUpload_Click(object sender, EventArgs e) 
{ 
if (FileUpload1.PostedFile != null) 
{ 
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);   
FileUpload1.SaveAs(Server.MapPath("upload/" + FileName));   
String strConnString = System.Configuration.ConfigurationManager 
.ConnectionStrings["conString"].ConnectionString; 
SqlConnection con = new SqlConnection(strConnString); 
string strQuery = "insert into tblFiles (Name, FilePath)" + 
" values(@FileName, @FilePath)"; 
SqlCommand cmd = new SqlCommand(strQuery); 
cmd.Parameters.AddWithValue("@FileName", FileName); 
cmd.Parameters.AddWithValue("@FilePath", "upload/" + FileName); 
cmd.CommandType = CommandType.Text; 
cmd.Connection = con; 
try 
{ 
con.Open(); 
cmd.ExecuteNonQuery(); 
} 
catch (Exception ex) 
{ 
Response.Write(ex.Message);    
} 
finally 
{    
con.Close(); 
con.Dispose(); 
} 
} 
} 

// show data in grid 
public void shofile2() 
{ 
DataTable dt = new DataTable(); 
String strConnString = System.Configuration.ConfigurationManager. 
ConnectionStrings["conString"].ConnectionString; 
string strQuery = "select * from tblFiles where UnqId = '" + UnqId1.Text + "'"; 
SqlCommand cmd = new SqlCommand(strQuery); 
SqlConnection con = new SqlConnection(strConnString); 
SqlDataAdapter sda = new SqlDataAdapter(); 
cmd.CommandType = CommandType.Text; 
cmd.Connection = con; 
try 
{ 
con.Open(); 
sda.SelectCommand = cmd; 
sda.Fill(dt); 
GridView1.DataSource = dt; 
GridView1.DataBind(); 
} 
catch (Exception ex) 
{ 
Response.Write(ex.Message); 
} 
finally 
{ 
con.Close(); 
sda.Dispose(); 
con.Dispose(); 
} 
} 

// just trying for download 
protected void DownloadFile(object sender, EventArgs e) 
{ 
if (con.State != ConnectionState.Closed) 
{ 
con.Close(); 
} 
con.Open(); 
SqlCommand cmd = new SqlCommand("select * from tblFiles", con); 
SqlDataReader DR1 = cmd.ExecuteReader(); 
if (DR1.Read()) 
{ 
//Label40.Visible = true; 
//Label40.Text = DR1.GetValue(5).ToString(); 
string filePath = DR1.GetValue(2).ToString(); 
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); 
Response.TransmitFile(Server.MapPath(filePath)); 
Response.End(); 
} 
} 

//表

CREATE TABLE dbo . tblFiles (
id int IDENTITY(8184,1) NOT NULL, 
Name varchar (500) NULL, 
FilePath varchar (500) NULL, 
ContentType varchar (500) NULL, 
Data varbinary (max) NULL, 
UnqId int NULL, 
remarks varchar (500) NULL, 
CONSTRAINT PK_tblFiles PRIMARY KEY CLUSTERED 
(
id ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON PRIMARY 
) ON PRIMARY TEXTIMAGE_ON PRIMARY 

GO 
ALTER TABLE dbo . tblFiles WITH CHECK ADD FOREIGN KEY(UnqId) 
REFERENCES dbo . pmsbl (UnqId) 
GO 

回答

0

u能通過改變Response.TransmitFile(使用Server.Mappath(文件路徑))嘗試;與

Response.WriteFile(Server.MapPath(filePath)); 
+0

感謝您的快速幫助。是的,它正在下載文件,但它也正在更改文件名和擴展名。我的文件名是aa.txt,並將其下載爲「upload_aa.txt-1.html」 –

+0

file:/// C:/Users/BREAKT~1/AppData/Local/Temp/upload_aa.txt-1.html ----現在顯示一個錯誤----------- DataBinding:'System.Data.DataRowView'不包含名稱爲'Value'的屬性..sr1708899235 –

+0

相關問題