MVC3 Download Excel Workbook from CSV data - asp.net-mvc-3

I've got a set of data that exists in memory in a CSV format. I have this method in my controller:
public FileContentResult ItemsAsExcelExport(){
var model = _itemService.GetExcelExportModel();
return new FileContentResult(model.CSVData, model.MimeType){FileDownloadName = model.FileName};
}
The problem here is that my model.CSVData property returns a simple comma delimited set of values. I'm not sure how I can satisfy the fileContents argument of the FileContentResult contructor. It's asking for a byte array.
Thanks in advance.

Take a look at this question How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
The solution is
byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);

Related

SWIFT - String based Key-Value Array decoding?

I have a String based Key-Value Array inside of a String, and I want to decoded it and assign the value to an existing array in Swift 4.2. For example:
let array: [String:String] = []
let stringToDecode = “[\“Hello\”:\”World\”, \"Key\":\"Value\"]”
// I want ‘array’ to be assigned
// to the value that is inside
// ‘stringToDecode’
I’ve tried the JSON decoder, but it couldn’t decode it. Is there a simple way to do this? Thank you.
Try using a library like SwiftyJson, it makes working with json much easier.

How to get XPages and JSON to not put variable names in Uppercase

I'm trying to do the following update using XPages Extension Library.
#{javascript:var mydata = {
product: getComponent("inputProduct").getValue()
};
var params = [1, 2];,
#JdbcUpdate("mssql","table_name",mydata,"order_no=? AND order_line_no=?",params)};
I get the error:
Error while executing JavaScript action expression
Script interpreter error, line=6, col=1: Error while executing function '#JdbcUpdate'
Invalid column name 'PRODUCT'.
The problem is that XPages when it converts the JSON it puts product to PRODUCT.
Can you set the extension library to respect the case of the JSON and not convert to Uppercase? Or can anyone point to where this setting could be set if not the extension library?
Thanks
The problem is com.ibm.xsp.extlib.util.JdbcUtil.appendColumnName()
public static void appendColumnName(StringBuilder b, String colName) {
colName = colName.toUpperCase();
b.append(colName);
}
This just needs changing to not upper case the variable.
There may be other methods that need changing if other variables are getting upper cased.

File to Byte Array in WinJS

I'm tinkering with some Windows Store development in JavaScript and I seem to be stuck on how to get a byte array from a binary file. I've found a couple of examples online, but they all seem to only read in text whereas my file is an image. I'm opening the file like this:
Windows.Storage.FileIO.readBufferAsync(photos[currentIndex]).done(function (buffer) {
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
var fileContent = dataReader.readString(buffer.length);
dataReader.close();
// do something with fileContent
});
Where photos[currentIndex] is a file (loaded from getFilesAsync()). The error in this case, of course, is that readString fails on binary data. It can't map the "characters" into a string. I also tried this:
Windows.Storage.FileIO.readBufferAsync(photos[currentIndex]).done(function (buffer) {
var bytes = [];
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
dataReader.readBytes(bytes);
dataReader.close();
// do something with bytes
});
But bytes is empty, so I think I'm using this incorrectly. I imagine I'm just overlooking something simple here, but for some reason I just can't seem to find the right way to read a binary file into a byte array. Can somebody offer a second set of eyes to help?
Figured it out almost immediately after posting the question, but I figure I'll leave the answer here for posterity...
I needed to declare the array in the second example differently:
Windows.Storage.FileIO.readBufferAsync(photos[currentIndex]).done(function (buffer) {
var bytes = new Uint8Array(buffer.length);
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
dataReader.readBytes(bytes);
dataReader.close();
// do something with bytes
});
My JavaScript isn't quite up to par, so I guess I didn't understand how the array declaration was supposed to work. (When I do vanilla JavaScript in a browser, I always just declare empty arrays like I originally did and append to them.) But this does the trick.

How do I use a guid in a mongodb shell query

When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).
The following format doesn't work:
>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});
Thanks.
You can use easily:
.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
You have to compare the _id value against an instance of BinData (not against a string). Unfortunately the BinData constructor takes a Base64 string instead of a hex string.
Your GUID value is missing two hex digits at the end, so for the purposes of this example I will assume they are "00". The following values are equivalent:
hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)
base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="
So your query should be:
>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})
I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?
You could use the following js function in front of your query like so:
function LUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // removes extra characters
return new UUID(hex); //creates new UUID
}
db.person.find({"_id" : LUUID("E3E45566-AFE4-A564-7876-AEFF6745FF"});
You could save the function in .js file and load it or open it before you make your query and if you copy the value from your results you should rename the function with:
LUUID for Legacy UUID
JUUID for Java encoding
NUUID for .net encoding
CSUUID for c# encoding
PYUUID for python encoding
I know it's an old issue, but without any additional needs you can use this one:
find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})
You can fix this issue by using split() and join() workaround:
for instance if I use "E3E45566-AFE4-A564-7876-AEFF6745FF" hex value with - inside UUID() function, it does not return BinData in mongo so please try removing all the - before passing to UUID function.
db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});
Or by defining a variable to do it in multiple line:
var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});
or by creating a simple function:
function BUUID(uuid){
var str = uuid.split("-").join('');
return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();

MSXML2.DOMDocument.xml give me malformed xml

We have an old legacy system that where a component is writter in VB6. One method returns a string that is xml data. The xml data is created with msxml3.dll MSXML2.DOMDocument and returns the data of the document with the property xml: http://msdn.microsoft.com/en-us/library/ms755989(v=VS.85).aspx
However, some data of the xmldocument is from the database and one field is a hashed password string. The code that set the data for the element:
Set cellNode = rowNode.appendChild(xml.createElement("COL"))
If IsNull(rs(oField.name).Value) Then
cellNode.Text = ""
Else
cellNode.Text = rs(oField.name).Value
End If
This gives me malformed/non-wellformed xml:
<ROWS><ROW><COL>r<í</COL></ROW></ROWS>
Is there a workaround for this?
You should escape unicode characters. Or put them in a CDATA tag (which is not such a nice solution though)
Btw < > and & should be escaped as well.

Resources