C# 如何从网格视图上传和下载文件?

C# 如何从网格视图上传和下载文件?,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图从grid view下载文件,但遇到如下错误: 找不到路径“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,

我试图从grid view下载文件,但遇到如下错误:

找不到路径“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();
        }


    }

上载和删除文件所有功能都正常工作,但在下载过程中遇到问题。

错误消息会告诉您问题所在:

找不到路径“C:\Users\love\Desktop\Crime”的一部分 管理系统\犯罪管理 System\Admin\Data\~\Admin\Data\State and Capital list of India.pdf'

Admin\Data\~\Admin\Data->您可以看到Server.MapPath()已经映射到\Admin\Data,然后您尝试再次映射它,这会导致找不到路径。(您可以尝试输入资源管理器中给定的路径-无效)

如果仅将文件名添加到Server.MapPath(),则应获得正确的下载路径:

Response.TransmitFile(Server.MapPath(e.CommandArgument.ToString()));

如果我这样做,则在构建过程中会出现错误,如:错误5“System.Web.HttpServerUtility.MapPath(string)”的最佳重载方法匹配'具有一些无效参数C:\Users\love\Desktop\Crime Management System\Crime Management System\Admin\ManageFileManager.aspx.cs 104 39 Crime Management systems如果这样做,则在生成过程中会出现错误,如:错误5与'System.Web.HttpServerUtility.MapPath(字符串)匹配的最佳重载方法“有一些是无效的arguments@LoveAajkal如果答案对你有帮助,请投票/接受它,以便其他用户知道什么是有帮助的。