【问题标题】:ASP.Net download file from folder based on the file path saved in sql databaseASP.Net根据保存在sql数据库中的文件路径从文件夹下载文件
【发布时间】:2018-02-08 01:23:33
【问题描述】:

我正在上传文件夹中的文件并将带有文件名的路径保存在 ms sql 数据库中。我想根据保存在 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

【问题讨论】:

    标签: asp.net sql-server file-upload directory


    【解决方案1】:

    你可以尝试改变 Response.TransmitFile(Server.MapPath(filePath));与

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

    【讨论】:

    • 感谢您的快速帮助。是的,它正在下载文件,但它也在更改文件名和扩展名。我的文件名是 aa.txt,它正在下载为“upload_aa.txt-1.html”
    • file:///C:/Users/BREAKT~1/AppData/Local/Temp/upload_aa.txt-1.html ----现在显示一个错误-------- ----DataBinding:“System.Data.DataRowView”不包含名为“Value”的属性.sr1708899235
    • ​​ton>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多