如何提供下载文件的自定义位置
本文关键字:自定义 位置 文件 何提供 下载 | 更新日期: 2023-09-27 18:15:37
我只是想知道我怎么能给一个自定义的默认位置抓取文件。我已经上传了一个文件到本地数据库,我已经将文件绑定到网格。当我按下下载,它显示一个错误叫做"文件不在位置"
如果我复制特定上传的文件到指定的位置,我可以很容易地下载它。所以我只需要知道我怎么能给一个默认的位置,这样我就可以上传和下载文件从相同的确切位置。
错误快照:https://imageshack.com/i/ewTrmAI2j
编辑相同的代码与自定义的文件夹路径。但我不知道为什么文件总是被要求从bin/debug/文件夹。为什么会这样。有什么办法我可以改变这个文件夹…除了bin/debug/文件夹
代码:
private void DownloadAttachment(DataGridViewCell dgvCell)
{
string fileName = Convert.ToString(dgvCell.Value);
//Return if the cell is empty
if (fileName == string.Empty)
return;
FileInfo fileInfo = new FileInfo(fileName);
string fileExtension = fileInfo.Extension;
byte[] byteData = File.ReadAllBytes(fileInfo.FullName); - - - - <<<<< ERROR HERE
//show save as dialog
using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
//Set Save dialog properties
saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
saveFileDialog1.Title = "Save File as";
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.FileName = fileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string s = cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value.ToString();
File.WriteAllBytes(saveFileDialog1.FileName, byteData);
byteData = System.Text.Encoding.ASCII.GetBytes(s);
}
}
}
FileInfo()构造函数只适用于完整的文件路径。这听起来就像您试图仅使用文件名的构造函数,此时当您试图读取文件时,它会失败,因为它不是有效路径。有两种方法可以处理这个问题:
-
创建您自己的MyFileInfo()类,继承FileInfo(),并添加一个构造函数,将您的特定路径附加到文件名。
-
只需在代码中添加内联路径:
var myPath = @"c:'folder'stuff'";FileInfo = new FileInfo(myPath + fileName);
通常,路径会在app.config中设置,以便您可以在需要时轻松更改它。
我找到了答案
为gridview的绑定文件路径编码,并使用文件路径
下载该文件。private void UploadAttachment(DataGridViewCell dgvCell)
{
using (OpenFileDialog fileDialog = new OpenFileDialog())
{
//Set File dialog properties
fileDialog.CheckFileExists = true;
fileDialog.CheckPathExists = true;
fileDialog.Filter = "All Files|*.*";
fileDialog.Title = "Select a file";
fileDialog.Multiselect = true;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string strfilename = fileDialog.FileName;
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = strfilename;
}
}
}
/// <summary>
/// Download Attachment from the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void DownloadAttachment(DataGridViewCell dgvCell)
{
string fileName = Convert.ToString(dgvCell.Value);
if (!string.IsNullOrEmpty(fileName))
{
byte[] objData;
FileInfo fileInfo = new FileInfo(fileName);
string fileExtension = fileInfo.Extension;
//show save as dialog
using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
//Set Save dialog properties
saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
saveFileDialog1.Title = "Save File as";
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.FileName = fileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string s = cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value.ToString();
objData = File.ReadAllBytes(s);
File.WriteAllBytes(saveFileDialog1.FileName, objData);
}
}
}
}
}