简体   繁体   中英

How to solve my error when using FileStream to save selected PDF from Adobe PDF reader?

I am using Adobe PDF Reader to view scanned paper from my computer and try to save it to database in my windows form application.

I select the PDF file by using open file dialog , Then I need to save it in my database. I am using the following code:

private void btnPDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PDFViewer.src = ofd.FileName;
            }
        }
private void btnsave_Click(object sender, EventArgs e)
        {
            MemoryStream msins = new MemoryStream();
            MemoryStream msid = new MemoryStream();
            pictureInsurance.Image.Save(msins, pictureInsurance.Image.RawFormat);
            pictureIDIQAMA.Image.Save(msid, pictureIDIQAMA.Image.RawFormat);
            byte[] byteInsurance = msins.ToArray();
            byte[] byteId = msid.ToArray();
            FileStream fStream = File.OpenRead("C:\\myfile.pdf");
            byte[] Doccontents = new byte[fStream.Length];
            fStream.Read(Doccontents, 0, (int)fStream.Length);
            fStream.Close();

            if (update == 1)
            {
                patient.ADD_PATIENT(textName.Text,
                                Convert.ToInt32(textAge.Text),
                                textMobile.Text,
                                textEmail.Text, textaddress.Text,
                                Convert.ToInt32(combogender.SelectedValue),
                                textIDNO.Text,
                                Convert.ToInt32(comboNat.SelectedValue), 
                                byteInsurance,byteId,Doccontents);
                MessageBox.Show("Patient Added Successfully", "ADD PATIENT", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

When i use the fixed path its save the content in the database :

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

but when i use the name of Adobe PDF reader to save the selected PDF file its show the following error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: URI formats are not supported."

This is the code with error :

FileStream fStream = File.OpenRead(PDFViewer.src);

And with this fix path it saved without error:

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

This is the part code for my void parameters calling stored procedure and varbinary column is it correct ?

public void ADD_PATIENT(string DocContent )
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            SqlParameter[] param = new SqlParameter[11];

            param[10] = new SqlParameter("@DocContent", SqlDbType.VarBinary);
            param[10].Value = DocContent;

            DAL.ExecuteCommand("ADD_PATIENT", param);
            DAL.close();

        }

What is the changes in code i need to do to save selected file instead of PDFViewer.src or how can i use byte[] method and memory stream like images to save PDF in the SQL server database ?

The argument of File.OpenRead() requires a string. If PDFViewer.src is not of the type string but of the type URI (or similar) then you get the problem which you are mentioning. Make sure that PDFViewer.src is a string (containing a valid path and filename) or cast it to a string, if possible:

 FileStream fStream = File.OpenRead((PDFViewer.src).ToString());

or

 FileStream fStream = File.OpenRead((string)PDFViewer.src);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM