Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
folder path is saved in a table database
i have to show all the datas from the table in datagridview
and when click the "file_name.pdf " ,that pdf file has to be downloaded
please help

i got the error that adobe reader does not read the pdf file and something like that
"Adobe reader coudln't read downloaed pdf file in C# datagridview cellcontent click":


What I have tried:

  string Calibaration = gvCalibration.Rows[e.RowIndex].Cells["CalibrationCertificate"].Value.ToString();
string targetPath = System.Windows.Forms.Application.StartupPath + "\\App_Data\\CalibrationCertificate\\";
string Calfilename;
 Calfilename = Path.GetFileName(Calibaration);
 string extn = Path.GetExtension(Calibaration);
  var byteData = Encoding.UTF8.GetBytes(Calfilename);

using (SaveFileDialog sfd = new SaveFileDialog())
    {
 //Set Save dialog properties
  //sfd.Filter = "Text files (*.txt) | *.txt | All files(*.*) | *.* ";
 sfd.Filter = "Files (*" + extn + ")|*" + extn;
sfd.Title = "Save File as";
 sfd.CheckPathExists = true;
 sfd.RestoreDirectory = true;
  sfd.FileName = Calfilename;

 if (sfd.ShowDialog() != DialogResult.OK || string.IsNullOrEmpty(sfd.FileName))
                            {
                                return;
                            }

 using (var saveFileDialogStream = sfd.OpenFile())
                            {
  saveFileDialogStream.Write(byteData, 0, byteData.Length);
                            }

}
Posted
Updated 6-Apr-22 22:37pm

1 solution

If you're saying the file is saved successfully but Adobe Reader is throwing an error it's probably because you're not actually copying the file correctly. If you look here:
C#
var byteData = Encoding.UTF8.GetBytes(Calfilename);

using (var saveFileDialogStream = sfd.OpenFile())
{
  saveFileDialogStream.Write(byteData, 0, byteData.Length);
}

The value of byteData here isn't the contents of the file, Encoding.UTF8.GetBytes() converts whatever value is passed into it into a byte array, it doesn't read the contents of the file.

Instead you need to use the variables you've instantiated in the code. I assume that targetPath is where the files are stored, and Calfilename is the name of the file, so to get the file contents you can do:
C#
var byteData = File.ReadAllBytes(targetPath + Calfilename);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900