c# - 找不到路径“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);
错误在哪里?
解决方案
没有理由使用 WebClient 从您的电脑“下载”文件,直接将其作为文件读取,而不是替换
WebClient user = new WebClient();
Byte[] filebuffer = user.DownloadData(filepath);
和
Byte[] filebuffer = System.IO.File.ReadAllBytes(filepath);
推荐阅读
- c# - UWP 中的 SelectedItems ListView 绑定失败
- c# - 如何在 C# 中反映字符串类型的不同委托?
- javascript - 为什么我在编辑创建后无法添加新笔记?
- google-cloud-platform - Google Cloud Platform 抢占式实例:磁盘会发生什么?
- windows - 如何启动默认浏览器以打开本地 .html 文件
- sql - 将外部表中的两列复制到另一个表中
- c - sscanf 和 %i / 大无符号整数的基数检测
- c# - EF Core 3.1 更新实体
- php - 从php中的字符串格式化json数据
- airflow - 使用 PythonOperator 的输出从气流到另一个 PythonOperator