Converting byte[] to zpl format - asp.net

I am accessing the shipping APIs and getting label information back in a byte[] format.
If I save this as a file, opening the file reveals the beautiful data that I need.
I have been saving this file like this:
string LabelPath = "c:\\temp\\";
string LabelFileName = LabelPath + "trackingnumber" + ".zpl";
FileStream LabelFile = new FileStream(LabelFileName, FileMode.Create);
LabelFile.Write(Label.Parts[0].Image, 0, Label.Parts[0].Image.Length);
LabelFile.Close();
I want to be able to do whatever conversion this file saving is doing to be able to pass the information as pure data. The data on file renders something like this:
^FO28,962^A0N,27,32^FWN^FH^FD## MASTER ## ^FS
^FO136,874^A0N,27,36^FWN^FH^FD1 of 2^FS
^FO32,253^AdN,0,0^FWN^FH^FDLouisville KY 40218^FS
This is what I have tried and it does convert successfully to a string but it doesn't look anything like I need it to. It's just a long string that our Zebra printer does not know how to handle.
LabelStream = Convert.ToBase64String(Label.Parts[0].Image)
How do I do the same kind of conversion that the LabelFile.Write is doing without having to actually save a file?

It looks your data is not base64, but plain text? Maybe you can try this?
LabelStream = Encoding.Default.GetString(Label.Parts[0].Image)

Related

How to convert xls to xlsx in vb.net?

I am using a resource file(abc.xls) in my project.
" IO.File.WriteAllBytes(tempPath, My.Resources.abc) "
But i want to use abc.xlsx instead of abc.xls without changing resourse file.
can I do saveAs? how?
or please give any other solution.
Thanks.......
Not sure what you exactly want to do, but I'll try to answer the second portion of your question.
'Make a path where you want to save the file
Dim Path as String
Path = "C:\Programs\blabla\"
Dim Final As String
Final = Path & "Filename.xlsx"
YourWorkBook.SaveAs(Final)
Or just try searching google for a more detailed explanation. (You've given little no none info, showing very little research made)
Try searching "How to save excel file in vb.net"

Base64 Encode a ZIP file using Classic ASP and VB Script

I have a zip file, which contains one CSV file.
I need to Base64 encode this zip file to send to eBay (using their API).
I used this website: http://www.opinionatedgeek.com/DotNet/Tools/Base64Encode/ which works nicely, I upload my zip file and it returns a base64 encoded string which eBay likes.
I need to do what this website does, but using Classic ASP and VB Script.
I already have a base64 encode function, from here: http://www.motobit.com/tips/detpg_base64encode/ so I don't need a script for that. This function takes a parameter, so I need to turn my zip file into a string (I think) to pass into this function.
I have tried using ADODB.Stream and the LoadFromFile method, but the string it returns, after base64 encoding, doesn't match that from the opinionated geek website and isn't accepted by eBay.
This is what I've tried:
Dim objStream, strFileText
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile Server.MapPath("myzipfile.zip")
strFileText = Base64Encode(objStream.Read)
Response.Write strFileText
objStream.Close
Set objStream = Nothing
Can anyone help..?
Thanks!
This is now solved...
I was missing the BinaryToString function between the stream output and the base64 encode.
Now I use:
strFileText = Base64Encode(BinaryToString(objStream.Read))
Where the new function is...
Function BinaryToString(Binary)
Dim I, S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function
The output from this now matches the output from the opinionated geek tool.
Thanks to ulluoink for pointing me in the right direction!

Extracting data from a tab-delimited file with JavaScript

I'm trying to extract data from a data file that's tab-delimited (in some parts), and really seems like it's going to be a headache to work out (I reallw wish they could just have CSV-ed it).
Here's the data:
http://www.fededirectory.frb.org/FedACHdir.txt
Here's the format description:
www.fededirectory.frb.org/format_ACH.cfm
I'd like to extract this data and store it in a database with serverside javascript, (ASP). Any ideas?
Your file is not tab delimited... it is position delimited.
To handle the file using javascript, the file must be on the same server and available via HTTP.
If you need to upload the file to some server, the server side language need to extract all the data based on your layout file
In order to extract it... you must for example do something like:
String line = "011000015O0110000150020802000000000FEDERAL RESERVE BANK 1000 PEACHTREE ST N.E. ATLANTA GA303094470866234568111 ";
String routingNumber = line.substring(0,8);
String officeCode = line.substring(8,9);
String servicingNumber = line.substring(9,17);
String typeCode = line.substring(17,18);
...
...
...
String filler = line.substring(151,line.length());
And iterate that code for every line in your file.
In pseudo-code :
for (Line line in File) {
// do the code above
}
Note: Process that file with JavaScript will be painful I recommend to do it in the server side of your application.

Fix Special Characters in String

I've got a program that in a nutshell reads values from a SQL database and writes them to a tab-delimited text file.
The issue is that some of the values in the database have special characters (TM, dash, ellipsis, etc.) When written to the text file, the formatting is lost and they come across as junk "™ or – etc"
When the value is viewed in the immediate window, before it is written to the txt file, everything looks fine. My guess is that this is an issue of encoding. But, I'm not real sure how to proceed, where to look, or what to look for.
Is this ASCII or UTF-8? If it's one of those how do I correct it before it's written to the text file.
Here's how I build the text file (where feedStr is a StringBuilder)
objReader = New StreamWriter(filePath)
objReader.Write(feedStr)
objReader.Close()
The default encoding for StreamWriter is UTF8 (with no byte order mark). Your result file is ok, the question is what do you open it in afterwards? If you open it in a UTF8 capable text editor, the characters should look the way you want.
You can also write the text file in another encoding, for example iso-8859-1 (latin1)
objReader = New StreamWriter(filePath, false, Encoding.GetEncoding("iso-8859-1"))

How to add encoding information to the response stream in ASP.NET?

I have following piece of code:
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/rtf; charset=UTF-8";
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-disposition", "attachment;filename=lista_obecnosci.csv");
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
}
When I try to open generated csv file, I get following behavior:
In Notepad2 - everything is fine.
In Word - conversion wizard opens and asks to convert the text. It suggest UTF-8, which is somehow ok.
In Excel - I get real mess. None of those Polish characters can be displayed.
I wanted to write those special encoding-information characters in front of my string, i.e.
context.Response.Write((char)0xef);
context.Response.Write((char)0xbb);
context.Response.Write((char)0xbf);
but that won't do any good. The response stream is treating that as normal data and converts it to something different.
I'd appreciate help on this one.
I ran into the same problem, and this was my solution:
context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
What you call "encoding-information" is actually a BOM. I suspect each of those "characters" is getting encoded separately. To write the BOM manually, you have to write it as three bytes, not three characters. I'm not familiar with the .NET I/O classes, but there should be a method available to you that takes a byte or byte[] parameter and writes them directly to the file.
By the way, the UTF-8 BOM is optional; in fact, its use is discouraged by the Unicode Consortium. If you don't have a specific reason for using it, save yourself some hassle and leave it out.
EDIT: I just remembered you can also write the actual BOM character, '\uFEFF', and let the encoder handle it:
context.Response.Write('\uFEFF');
I think the problem is with Excel based on Microsoft Excel mangles Diacritics in .csv files. To prove this, copy your sample output string of ąęćżźń󳥌ŻŹĆŃŁÓĘ and paste into a test file using your favorite editor, and save as a UTF-8 encoded .csv file. Open in Excel and see the same issues.
The answer from Alan Moore
translated to VB:
Context.Response.Write(""c)

Resources