【发布时间】:2015-04-08 13:44:40
【问题描述】:
我尝试从网格视图下载文件,但我遇到如下错误:
找不到路径“C:\Users\love\Desktop\Crime Management System\Crime Management System\Admin\Data\~\Admin\Data\State and Capital list of India.pdf”的一部分。
上传 Aspx 代码:
Property p = new Property();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillData();
}
}
private void FillData()
{
GridView1.DataSource = p.GetFile();
GridView1.DataBind();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
p.FileName = txtFileName.Text;
if (FileUpload1.HasFile)
{
p.Data = "~/Admin/Data/" + FileUpload1.PostedFile.FileName;
FileUpload1.SaveAs(Server.MapPath(p.Data));
}
else
p.Data = "Data is not Avilable";
}
p.CreateDate = Convert.ToDateTime(dtpdate.Text);
p.Size = txtSize.Text;
p.UploadFile(p);
Response.Write("Upload successfull");
}
源代码:
public void UploadFile(Property p)
{
cmd = new SqlCommand("UploadFile", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FileName", p.FileName );
cmd.Parameters.AddWithValue("@Data", p.Data);
cmd.Parameters.AddWithValue("@CreateDate", p.CreateDate);
cmd.Parameters.AddWithValue("@Size", p.Size);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void DeleteFile(Property p)
{
cmd = new SqlCommand("DeleteFile", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DataId", p.DataId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable GetFile()
{
cmd = new SqlCommand("Select * from tblData", con);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
return dt;
}
用于下载的 Aspx 代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Admin/Data/") + e.CommandArgument);
Response.End();
}
}
上传和删除文件一切正常,但在下载过程中遇到问题。
【问题讨论】:
-
如果我这样做了,那么在构建过程中会出现错误,例如:错误 5 'System.Web.HttpServerUtility.MapPath(string)' 的最佳重载方法匹配有一些无效参数 C:\Users\love\桌面\犯罪管理系统\犯罪管理系统\Admin\ManageFileManager.aspx.cs 104 39 犯罪管理系统