I am using a basic telerik export document to pdf function. this works great to export the page directly to the user. I then pass this to a controller as a string via datauri.
how can I convert it back to a file so that I can attach it to an email?
imageData: "data:application/pdf;base64,JVBERi0xLjQKJcLB2s/OCgoxIDAg...
I found a way to do this. replace a bit of the header, convert to byes, make a new stream, attach stream to email.
imageData = imageData.Replace("data:application/pdf;base64,", "");
byte[] bytes = Convert.FromBase64String(imageData);
Stream stream = new MemoryStream(bytes);
email.Attachments.AddFileAttachment("SALOR.pdf",stream);
Related
I'm trying to show a pdf in my view that I download from an API, however everytime the view is loaded the browser downloads the pdf instead of showing it inside the iframe or embed.
I'm getting and returning the pdf in my action like this:
public FileStreamResult GetPDFInBrowser(Guid id)
{
string url = A link that gets the pdf from API with id, content is stored as a string inside the "root" object;
WebClient client = new WebClient();
string response = client.DownloadString(url);
Root responseObject = JsonConvert.DeserializeObject<Root>(response);
OOLDocument oolDocument = responseObject.value[0];
byte[] byteArray = Encoding.UTF8.GetBytes(oolDocument.Contents);
MemoryStream stream = new MemoryStream(byteArray);
return File(stream, "application/pdf", oolDocument.FileName);
}
Afterwards I try to show the pdf in my view like this with either iframe or embed:
<iframe src="#baseUrl/GetPDFInBrowser?id=#Model.DocumentsGuid" class="pdfviewer">
</iframe>
<embed src="#baseUrl/GetPDFInBrowser?id=#Model.DocumentsGuid" class="pdfviewer" type="application/pdf" id="pdfObjectViewer">
</embed>
So, how do I make sure the pdf is embedded in the browser instead of having the browser download it?
Any help or tips for improving the code are greatly appreciated.
Edit:
With some more searching combined with trial and error I was able to fix the error I was getting and have therefore removed it from the remainder of the question.
For those interested i fixed the error "Cannot load pdf-document" by changing:
byte[] byteArray = Encoding.UTF8.GetBytes(oolDocument.Contents);
To:
byte[] byteArray = Convert.FromBase64String(oolDocument.Contents);
However the pdf is still being downloaded instead of being shown in the view
I am uploading an .mp3 file via FTP code using C#, the file is uploaded successfully on server but when i bind to a simple audio control or directly view in browser it does not work as expected, whereas when i upload manually on the server it works perfectly.
Code:
var inputStream = FileUpload1.PostedFile.InputStream;
byte[] fileBytes = new byte[inputStream.Length];
inputStream.Read(fileBytes, 0, fileBytes.Length);
Note: When i view the file in Firefox it shows MIME type is not supported.
Thanks!
You're reading the file as a string then using UTF8 encoding to turn it into bytes. If you do that, and the file contains any binary sequence that doesn't code to a valid UTF8 value, parts of the data stream will simply get discarded.
Instead, read it directly as bytes. Don't bother with the StreamReader. Call the Read() method on the underlying stream. Example:
var inputStream = FileUpload1.PostedFile.InputStream
byte[] fileBytes = new byte[inputStream.Length];
inputStream.Read(fileBytes, 0, fileStream.Length);
I've seen a ton of examples on the stack about converting a base64 string and/or URI but can't seem to get it to render the base64. I've tried using a uri and a string. Using the code below, i am able to render a pdf but when i try to open the pdf it says an error has occurred. Nothing is rendered. Any ideas?
string base64BinaryStr = "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlIC9YT2JqZWN0IC9T";
using (FileStream stream = System.IO.File.Create(#"c:\Users\signature.pdf"))
{
byte[] byteArray = Convert.FromBase64String(base64BinaryStr);
stream.Write(byteArray, 0, byteArray.Length);
}
**Here's a screenshot of what the file does when i try to open it:
I have a sequence of bytes as follows (Word format) exported from crystal report
Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
I would like to convert that stream to a PDF stream to be stored in a varbinary column in sql server database.
There is a barcode in the document and i am unable to embed it when I export to pdf. Exporting to word works though so I want to try from Word to PDF
Can anyone assist me in getting this PDF byte[]?
You can do that using the Microsoft.Office.Interop.Word NuGet Package. Once you added it on your application you can flush your Byte Array to a temporary file, then open the temp file with Interop.Word, work with it, save the result and read the result back into a Byte Array.
Here's a sample code snippet that does just that:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// Save DOCX into a PDF
var pdfPath = "path-to-pdf-file.pdf";
doc.SaveAs2(pdfPath, word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit(); // VERY IMPORTANT: do this to close the MS Word instance
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);
For more info regarding the topic you can also read this post on my blog.
I am sending base64 encoded image from client side using javascript (I am creating Screenshot uploader applet for asp.net application using http://supa.sourceforge.net/) and this sends an ajax request to server to store the image. At server I am using HttpContext in GenericHanlder in asp.net application.
How to convert image data from HttpContext to image at server?
First, you need to convert the base 64 back into bytes:
byte[] data = System.Convert.FromBase64String(fromBase64);
Then, you can load it into an instance of Image:
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);
If you want to save it to a file instead, use System.IO.File.WriteAllBytes
I needed to do something similar, but wanted to work directly with the InputStream, so used this to do the decoding:
// using System.Security.Cryptography
var stream = new CryptoStream(Request.InputStream, new FromBase64Transform(), CryptoStreamMode.Read);
var img = Image.FromStream(stream);