【问题标题】:How to save filename only to the gridview but I need path while downloading the file如何仅将文件名保存到gridview,但下载文件时我需要路径
【发布时间】:2014-08-01 02:54:47
【问题描述】:

我终于知道了如何使用路径下载文件。但我想知道如何才能将文件名仅保留在网格视图上。虽然我需要完整的下载路径。

在调试时,我发现我不能只在文件上传时保留文件名。由于它被带到下载部分。 如果我保留文件名,则文件名被带到下载部分,文件不下载。

谁能帮帮我

代码

 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);
                }
            }
        }
    }

}

【问题讨论】:

    标签: c# visual-studio-2010 visual-studio file-upload datagridview


    【解决方案1】:

    你的问题不清楚。但如果我说对了。为什么不使用列作为名称,使用隐藏/0 宽度列作为文件路径。它是一个网格,因此您可能有很多列。另外,我认为下面的代码应该返回完整路径,除非您修剪并且看不到您修剪的位置。

                string strfilename = fileDialog.FileName;
    

    要获取文件名,您可以使用路径

       string filenameOnly= System.IO.Path.GetFileName(strfilename);
    

    上面应该返回你的文件名,只有你可以检查here以便更好地理解。

    添加另一列并将宽度设置为 0 。下面的例子

      DataGridViewColumn column = dataGridView.Columns[0];
        column.Width = 0;
    cncInfoDataGridView.Columns.Add(column);
    

    将您的文件路径保存在新列宽度为 0 宽度并在下载期间检索。

    这里问了一个类似的问题。 How to extract file name from file path name?

    【讨论】:

    • 我没有按照你说的方式尝试。它也在新列中显示相同的路径。 string strfilename = fileDialog.FileName; cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = strfilename; string filenameOnly = System.IO.Path.GetFileName(strfilename); //filenameOnly = string.Empty; this.cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[5].Value = filenameOnly;/// place where the path of drawing is holded
    • 在字符串filenameOnly = System.IO.Path.GetFileName(strfilename); 处放置一个断点并查看filenameonly 的值。 filenameonly 只会返回文件名,对此表示怀疑。 (例如,file.doc、file.exe、file.xls 等)。打个断点,告诉我们值是什么。
    【解决方案2】:
    Dictionary<int, byte[]> _myAttachments;
    private void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                //Throw error if attachment cell is not selected.
                //make sure user select only single cell
                if (dataGridView1.SelectedCells.Count == 1 && dataGridView1.SelectedCells[0].ColumnIndex == 1)
                {
                    UploadAttachment(dataGridView1.SelectedCells[0]);
                }
                else
                    MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            //Throw error if attachment cell is not selected.
            //make sure user select only single cell
            //and the cell have a value in it
            if (dataGridView1.SelectedCells.Count == 1 && dataGridView1.SelectedCells[0].ColumnIndex == 1 && dataGridView1.SelectedCells[0].Value != null)
            {
                DownloadAttachment(dataGridView1.SelectedCells[0]);
            }
            else
                MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //Throw error if attachment cell is not selected.
            //make sure user select only single cell
            //and the cell have a value in it
            if (dataGridView1.SelectedCells.Count == 1 && dataGridView1.SelectedCells[0].ColumnIndex == 1 && dataGridView1.SelectedCells[0].Value != null)
            {
                DownloadAttachment(dataGridView1.SelectedCells[0]);
            }
            else
                MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        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 = false;
    
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                    byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
                    dataGridView1.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
    
                    if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                        _myAttachments[dgvCell.RowIndex] = binaryData;
                    else
                        _myAttachments.Add(dgvCell.RowIndex, binaryData);
                }
            }
        }
        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 = null;
    
            //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)
                {
                    byteData = _myAttachments[dgvCell.RowIndex];
                    File.WriteAllBytes(saveFileDialog1.FileName, byteData);
                }
            }
        }
    

    【讨论】:

    • 您应该添加一些解释为什么以及如何此代码解决提问者的问题。仅发布代码在这里被认为是低质量的。
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2021-01-15
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多