-
May 14th, 2010, 05:13 AM
#1
[RESOLVED] Convert string to string literal verbatim
I have converted a PDF to byte[]. I then converted that to string since it specifically to be saved in the database as nvarchar(MAX). I would however like to know how to convert it to string literal verbatim (following the format of @"text here") to enable me to escape the characters present in the string so that it can be inserted into the database.
Code:
protected void SavePDFtoDB()
{
// Save PDF to byte[]
mBytePDF = File.ReadAllBytes(msPath);
// Convert byte[] to string
String contents = ByteArrayToString(mBytePDF);
// Save PDF in Database in form of string
MainTable lMainTable = new MainTable();
lMainTable.Date = DateTime.Now;
lMainTable.Archive = 1;
lMainTable.Text = @contents; // PROBLEM: Trying to convert to string literal verbatim
SData.mWebService.setMainTable(lMainTable);
}
private string ByteArrayToString(byte[] arr)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetString(arr);
}
Any help would be much appreciated!
-
May 14th, 2010, 05:17 AM
#2
Re: Convert string to string literal verbatim
Convert the byte array to a base64 encoded string.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
May 14th, 2010, 10:41 AM
#3
Re: Convert string to string literal verbatim
Thank you, I used your suggested method. When I try to open the PDF I however get the error message that the data is presumably damaged. Could this have to do with converting it to a base64 encoded string?
Code:
protected void GeneratePDF(int value)
{
GetReport(miSelectedReportID);
string lsTraineeName = SData.mWebService.getTrainee(SData.miTraineeID).Surname;
string lsFileName = "Beurteilungsbogen_" + lsTraineeName + ".pdf";
// Create PDF on current path with doc name "test.pdf"
string lsPDFpath = Server.MapPath("~/" + lsFileName);
using (mStream = new FileStream(lsPDFpath, FileMode.Create))
{
ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
mBuffer = encoding.GetBytes(mMainTable.Text);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + lsFileName);
Response.AddHeader("Content-Length", mBuffer.Length.ToString());
Response.OutputStream.Write(mBuffer, 0, mBuffer.Length);
//Response.BinaryWrite(mBuffer);
Response.Flush();
Response.End();
// To open in a new window: by saving pdf first
Response.Write("<script type='text/javascript'>");
Response.Write("window.open('" + "~/" + lsFileName + "','_blank')");
Response.Write("</script>");
}
}
-
May 14th, 2010, 12:20 PM
#4
Re: Convert string to string literal verbatim
ASCII is not base64. Yes, you've corrupted your data. Use the BitConverter or Convert class, i forget where it is exactly.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
May 17th, 2010, 03:51 AM
#5
Re: Convert string to string literal verbatim
Thanks!!!!! It works perfectly using:
Code:
try {
mBuffer = Convert.FromBase64String(mMainTable.Text);
// mBuffer = encoding.GetBytes(mMainTable.Text);
} catch (FormatException)
{
// Do nothing
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|