首页 > 解决方案 > 找不到路径“D:\Directorate-website\Directorate\Directorate\Upload\Secret\”的一部分

问题描述

我将文件保存在我的 Web 表单项目 /Upload/Secret/ 的文件夹中,并且保存正确。我将文件名保存在数据库中。我尝试查看 pdf 文件,并为我的项目 .dbml 创建了新项目,然后将表格拖放到该项目上。现在,当我按下链接按钮查看我的文件时,出现了这个错误:找不到路径的一部分。

我从 youtube 获得此代码 https://www.youtube.com/watch?v=h7sswv6LyIw

protected void linkfilebtn_Click1(object sender, EventArgs e)
        {
            int Rowindex = ((GridViewRow)((sender as Control)).NamingContainer).RowIndex;
            string filelocation = DGDEPT.Rows[Rowindex].Cells[3].Text;
            string filepath = Server.MapPath("~/Upload/Secret/" + filelocation);

            WebClient user = new WebClient();
            Byte[] filebuffer = user.DownloadData(filepath);

            if (filebuffer != null)
            {
                Response.ContentType = ("application/pdf");
                Response.AddHeader("content-length", filebuffer.Length.ToString());
                Response.BinaryWrite(filebuffer);
            }
        }

预期结果查看 pdf 文件,但实际显示此行错误

Byte[] filebuffer = user.DownloadData(filepath);

错误在哪里?

标签: c#

解决方案


没有理由使用 WebClient 从您的电脑“下载”文件,直接将其作为文件读取,而不是替换

WebClient user = new WebClient();
Byte[] filebuffer = user.DownloadData(filepath);

Byte[] filebuffer = System.IO.File.ReadAllBytes(filepath);

推荐阅读