Use Byte[] was converted from a string and sent through webservice - c#

I convert a file to a byte array and then I transfer it using a web service. When I receive the byte array, I convert it back to a file.
It's works ok, but the problem is when I convert a string to byte, like this:
<b>My Content</b>
and transfer it through the web service, how can I get a file with an HTML format (like doc, docx...) like:
My Content
I can save it as .txt but it's not the result I want. Here is the code I used to save it:
public ActionResult Download(byte[] bytes)
{
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet,"Filename");
}

Related

Convert Base64 String to an Image File and Save it to File System?

I am trying to make a .Net 6 console application that would take in a base64string and then save it to the file system as an actual image file
Example
I have this image
https://cdn.pixabay.com/photo/2016/03/28/12/35/cat-1285634_960_720.png
I would have this image already as a base64 string.
Now I want to save to my file system as "cat-1285634_960_720.png"
I just can't figure out how to do it. All the examples I see say to use Image.Save() but I can't find that in .Net6 and looks like it is removed.
First convert the base64 string to a byte array and then use File.WriteAllBytes(...) to save it:
byte[] imageByteArray = Convert.FromBase64String(base64String);
File.WriteAllBytes("image.png", imageByteArray);

Conversion from binary to byte[] in c#

I have an attachment of a MIME file which is encoded in binary format.
That binary data from the file is stored in a string variable in my application. My task is to convert that string to byte[] so that i can pass this to a stream object to upload it in the Azure blob storage. I am using the below code. Code does not break but once the it gets uploaded to Azure blob and I try to download it shows file is corrupt.
attachmentContents is the string object which contains the binary data.
//# byteAttachmentData is the byte[]
byteAttachmentData = UTF8Encoding.UTF8.GetBytes(attachmentContents)
Sample of binary data(Not pasting the entire content)
PK ! ̘Շʁ ͇ [Content_Types].xml ( ԕ̪ðE󅾃ѶيڨƄɢХh򁊴ύ͇Ӥ񷝇Ω%ʃo򌽷ّͤdċh>֤l،XFZUمʾfo񣋂
Ąi
ŬƷ7ęׁɈmB˲D󄹐9h쁐%ԞŗࠠNɯѠ~?

Saving a byte array to PDF file with OfficeJs

Using OfficeJs I want to save a Word document as a PDF and post that file to an Api.
Office.context.document.getFileAsync will let you get the entire document in a choice of 3 formats:
compressed: returns the entire document (.pptx or .docx) in Office Open XML (OOXML) format as a byte array
pdf: returns the entire document in PDF format as a byte array
text: returns only the text of the document as a string. (Word only)
I am posting the PDF byte array to a WebApi action that looks like this:
public async Task<IHttpActionResult> Upload([FromBody]byte[] bytes)
{
File.WriteAllBytes(#"C:\temp\testpdf.pdf", bytes);
return Ok();
}
On inspection the byte array is the same array created by the getFileAsync from Office Js.
The problem is the file written in File.WriteAllBytes is corrupt. If I open it with notepad, it is a string of the bytes - 37,80,68,70,45,49,46,53,13,10,37... and so on.
Any idea why the method WriteAllBytes does not create a PDF file from the OfficeJS pdf byte stream?
UPDATE 25/5/16
As hawkeye #StefanHegny pointed out, the byte array appears to be Ascii characters. Converting each byte to char and writing that out to PDF like this creates a blank PDF, but on inspection with NotePad, the contents do like a like a PDF document, though quite different to that when saving the same .docx as a .pdf.
var content = "";
foreach (var b in model.Bytes)
{
content += (char) b;
}
File.WriteAllText(#"C:\temp\testpdf.pdf", content);
Also note, this is extremely slow - about 5 minutes for 500kb PDF byte array on my dev machine.
I had the same pdf empty problem, and it was because I was converting to string and writing string to file(encoding problem), I solved by sending to the c# code the comma separated byte codes instead of converting to string, parsing bytes and using File.WriteAllBytes()
C# code:
string[] strings = HttpUtility.HtmlDecode(pdf).Split(',');
byte[] bytes = strings.Select(s => byte.Parse(s)).ToArray();
System.IO.File.WriteAllBytes("filename.pdf", bytes);

Retrieve byte array from c# webservice into Qt

I use webservice written in c# that exposes bytearray of audio file (mp3 file) that is stored in database (using entity framework).
When I retrieve it in c# and save it into file using File.WriteAllBytes() I can listen to audio (file's size is 10kB).
I need to do the same with Qt. I parse the xml and save audio byte array to QByteArray like this:
QByteArray bytes = readValue().toUtf8();
where readValue() is QStringRef and then I save it to file
qint64 bytesWritten = file.write(bytes);
File hes 14kB then and I suppose that there is some format problem but not sure where.
I solved this problem. In webservice I return byte array converter to hex string
string hex = BitConverter.ToString(ba);
string hexString = hex.Replace("-", "");
then in Qt I parse data
QByteArray bytes = QByteArray::fromHex(readValue().toString().toLatin1()));
and save it to file
qint64 bytesWritten = file.write(bytes);

PDF content inside XML response file

I receive XML file that includes PDF content:
<pdf>
<pdfContent>JVBERi0xLjQKJaqrrK0KNCAwIG9iago8PCAvV.......
How can I save the content into PDF file?
I'm using C# 4.0
That string value is the PDF in base64. If you convert the base64 to a byte array you can just write that byte array to disk.
Convert.FromBase64String
var buffer = Convert.FromBase64String(xmlStringValue);
File.WriteAllBytes(yourFileName, buffer);
It looks like the pdf content is encoded in base64. You will have to decode it and save it to a file.
Edit: indeed, when I use base64 to encode a pdf file, the first few characters are JVBERi0x...
It seems encoded with Base64, But not sure. if it is, you can take that long string and convert with the function Convert.FromBase64. You will obtain a byte[] that you can save as the actual pdf.

Categories