hi
i have write this code for upload pdf to Sql DB :
private void uploadPDFFiles()
{
string filetype;
string filename;
filename = uploadFilesLinkTextBox.Text.Substring(Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf("\\")) + 1, uploadFilesLinkTextBox.Text.Length - (Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf("\\")) + 1));
filetype = uploadFilesLinkTextBox.Text.Substring(Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf(".")) + 1, uploadFilesLinkTextBox.Text.Length - (Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf(".")) + 1));
MessageBox.Show(filename + " " + filetype);
//Validate user upload only specific bytes - un comment below lines if you need to validate only PDF files
if (filetype.ToUpper() != "PDF")
{
alertLabel.ForeColor = Color.Red;
alertLabel.Text = "الرجاء إدخال ملف PDF فقط ...";
return;
}
try
{
// Open file to read using file path
FileStream FS = new FileStream(uploadFilesLinkTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Add filestream to binary reader
BinaryReader BR = new BinaryReader(FS);
// get total byte length of the file
long allbytes = new FileInfo(uploadFilesLinkTextBox.Text).Length;
// read entire file into buffer
FileBytes = BR.ReadBytes((Int32)allbytes);
// close all instances
FS.Close();
FS.Dispose();
BR.Close();
}
catch (Exception ex)
{
alertLabel.Text = "Error during File Read " + ex.ToString();
//MessageBox.Show("Error during File Read " + ex.ToString());
}
}
inserting all my file bytes inside FileBytes variable and then stored it inside DB
then i download it from db using this code :
private void DownloadPDFFile(DataRow dr)
{
string id;
FileStream FS = null;
byte[] dbbyte;
try
{
//Get a stored PDF bytes
dbbyte = (byte[])dr["UploadFiles"];
//store file Temporarily
string filepath = @"C:\Users\Administrator.minfo-HP\Downloads\NewsAgency.pdf";
//Assign File path create file
FS = new FileStream(filepath, System.IO.FileMode.Create);
//Write bytes to create file
FS.Write(dbbyte, 0, dbbyte.Length);
// Open file after write
//Create instance for process class
Process Proc = new Process();
//assign file path for process
Proc.StartInfo.FileName = filepath;
Proc.Start();
}
catch(Exception ex)
{
throw new System.ArgumentException(ex.Message);
}
finally
{
//Close FileStream instance
FS.Close();
}
}
when i run this code it give me corrupted pdf downloaded in target folder and does not open, and the file size not the same as original pdf
where is the wrong in my code ???