I'm trying to display the results of a FileContentResult MVC Action in an object tag. I can easily display files that have a preview option; .txt, .pdf, .jpg, etc. The problem comes in when there are files that don't have a preview option. My problem isn't figuring out which type of files work, my problem is figuring out how to stop them from downloading (or trying to download).I've tried making an ajax call to grab the file, which I can do, but I don't know how to display it after that. Any tips or ideas? This is the object I get back from the jquery call.
It's essentially just the mimetype, the name, and the byte array. Here's a snap of the object in MVC:This doesn't seem like it should be as hard as it seems. Am I just missing something obvious? Thanks!
I realized what I could do in order to get around this problem, I won't submit this as the answer in case other people actually do have ideas on how to work around this. I think my solution is pretty self-explanatory, but I'll go into a little detail.
public ActionResult GetFile()
{
HBSFile file = new Common.Business.FileIO.FileIO(Server.MapPath("~/Content/Images"), "testdoc.docx").Read();
if (file.CanBePreviewed)
{
return File(file.Stream, file.ContentType);//, file.FileName + "." + file.Extension);
}
return null;
//return Json(new Test(File(file.Stream, file.ContentType, file.FileName + "." + file.Extension), file.CanBePreviewed), JsonRequestBehavior.AllowGet);
}
The HBSFile object is simply the properties of the file. file.Stream is a byte array. To get around my problem, I simply check to see if the file can be previewed or not. If it can be, I'll return a FileContentResult.
Related
I'm creating an android app which takes in some json data, is there a way to set up a directory such as;
http://......./jsons/*.json
Alternatively, a way to add into a json file called a.json, and extend its number of containing array data, pretty much add more data into the .json file this increase its size.
It could be by PHP or Javascript.
Look into Parsing JSON, you can use the JSON.parse() function, in addition, I'm not sure about getting all your JSON files from a directory call, maybe someone else will explain that.
var data ='{"name":"Ray Wlison",
"position":"Staff Author",
"courses":[
"JavaScript & Ajax",
"Buildinf Facebook Apps"]}';
var info = JSON.parse(data);
//var infostoring = JSON.stringify(info);
One way to add to a json file is to parse it, add to it, then save it again. This might not be optimal if you have large amounts of data but in that case you'll probably want a proper database anyway (like mongo).
Using PHP:
$json_data = json_decode(file_get_contents('a.json'));
array_push($json_data, 'some value');
file_put_contents('a.json', json_encode($json_data));
So, have been trying to get a javascript to read a spreadsheet, but needed it private rather than public, so spent quite a bit of time trying to figure out how to do that. It is actually quite easy, or seems like it is, except that I still can't read the spreadsheet. Will cover private spreadsheets through the cloud console in the end.
So, I have a rather simple spreadsheet which contains about 6 columns, (including customer emails and similar, hence why private). And I have been playing about trying to get it to work for the last couple of days.
First, the string!
So to enable this I first need a string that will return a json object (is that what I am expecting back?)
https address: https://spreadsheets.google.com/feeds/list/
Sheet ID :
Additional : /od6/private/full
Command : ?alt=json-in-script&callback=importGSS
or full string : https://spreadsheets.google.com/feeds/list//od6/private/full?alt=json-in-script&callback=importGSS
This seems to work quite fine, if I remove the json part it shows me the data sheet, if I ad it, I get an empty file instead.
So now to the code.
The function that SHOULD be returning the object is the following (spLink) contains the link above.
function loadData(spLink){
$.getJSON(spLink).always(function(data) {
console.log("Object created!");
console.log(data);
}).fail(function(message) {
console.error('Something went pretty wrong!');
console.error(message);
}).done(function(){
console.log('Done!');
});
}
The only thing I can really tell about it is that data continues returning null, and I can't really figure out where else the data may end up.
Have tried both with and without callback.
The list feed only works if there are no empty cells in a row with data, among other pitfalls.
Use the cell feed instead, which requires more code because you wont get the data organized by rows.
I have an html site with a form in it and I want the user to be able to create a text/xml file depending on the input. But I wan't to avoid setting up a webserver only for this task.
Is there a good way, to do that, e.g. with Javascript? I think you can't create files with Javascript, but maybe create a data url and pass the text, so the user can save it to file?
Or is there another way to achieve this simple task without a webserver?
Solved it, somehow. I create a data url data:text/xml;charset=utf-8, followed by the XML.
function createXML() {
var XML = 'data:text/xml;charset=utf-8,<MainNode>';
var elements = document.getElementsByTagName('input'),i;
for (i in elements) {
if (elements[i].checked == true) {
XML += elements[i].value;
}
}
XML += '</MainNode>';
window.open(XML);
}
So the url looks like data:text/xml;charset=utf-8,<MainNode><SubNode>...</SubNode>...</MainNode>
Unfortunately this doesn't work for me on Chromium(Chrome) and on Firefox. It just displays the XML instead of showing a save dialog. But I think that's because of my settings and at least you can save it as a XML-file manually.
I haven't tried this but it should work.
After getting form data, system will call page A.
page A will have javascript that gets query strings and builds the page accordingly.
After finishing page build, user can save current page with following statement in javascript
document.execCommand('SaveAs',true,'file.html');
I am semi-new to ASP.NET MVC. I am building an app that is used internally for my company.
The scenario is this: There are two Html.Listbox's. One has all database information, and the other is initally empty. The user would add items from the database listbox to the empty listbox.
Every time the user adds a command, I call a js function that calls an ActionResult "AddCommand" in my EditController. In the controller, the selected items that are added are saved to another database table.
Here is the code (this gets called every time an item is added):
function Add(listbox) {
...
//skipping initializing code for berevity
var url = "/Edit/AddCommand/" + cmd;
$.post(url);
}
So the problem occurs when the 'cmd' is an item that has a '/', ':', '%', '?', etc (some kind of special character)
So what I'm wondering is, what's the best way to escape these characters? Right now I'm checking the database's listbox item's text, and rebuilding the string, then in the Controller, I'm taking that built string and turning it back into its original state.
So for example, if the item they are adding is 'Cats/Dogs', I am posting 'Cats[SLASH]Dogs' to the controller, and in the controller changing it back to 'Cats/Dogs'.
Obviously this is a horrible hack, so I must be missing something. Any help would be greatly appreciated.
Why not just take this out of the URI? You're doing a POST, so put it in the form.
If your action is:
public ActionResult AddCommand(string cmd) { // ...
...then you can do:
var url = "/Edit/AddCommand";
var data = { cmd: cmd };
$.post(url, data);
... and everything will "just work" with no separate encoding step.
Have you tried using the 'escape' function, before sending the data? This way, all special characters are encoded in safe characters. On the server-side, you can decode the value.
function Add(listbox) { ...
//skipping initializing code for berevity
var url = "/Edit/AddCommand/" + escape(cmd);
$.post(url);
}
use javascript escaping, it does urlencoding.
Javascript encoding
Then in C# you can simple decode it.
It will look as such
function Add(listbox) { ...
//skipping initializing code for berevity
var url = "/Edit/AddCommand/" + escape(cmd);
$.post(url);
}
Have you tried just wrapping your cmd variable in a call to escape()?
You could pass the details as a query string. At the moment I'm guessing you action looks like:
public virtual ActionResult AddCommand( string id )
you could change it to:
public virtual ActionResult AddCommand( string cmd )
and then in you javascript call:
var url = "/Edit/AddCommand?cmd=" + cmd;
That way you don't need to worry about the encoding.
A better way would be if you could pass the databases item id rather than a string. This would probably be better performance for your db as well.
I've been trying to fix this for two hours straight and I can't figure it out.
onclick = "location='exceltest.asp?vanjaar=<%=vanjaar%>&vanmaand=<%=vanmaand%>&vandag=<%=vandag%>&totjaar=<%=totjaar%>&totmaand=<%=totmaand%>&totdag=<%=totdag%>'"
That line of code is in an < input type="button" /> attribute. The button links to a page where an Excel download should be triggered. The values in the URL are from- and to-date-parts. (year, month, day)
this:
onclick = "location='exceltest.asp?fromdate=<%=fromdate%>&todate=<%=todate%>'" />
does not work, because somehow IE7 reads the date (eg. 2008/1/1) wrong. Something to do with the slashes I think.
But when I try to click the button in IE and thus download the generated file, Internet explorer tries do download the file
exceltest.asp?vanjaar=2008vanmaand=1vandag=1totjaar=2008totmaand=2totdag=1
instead of the excel file I want.
FF offers to download the excelfile, but gives (in that excelfile) an overview of an htmlpage with an errormessage telling me my query is wrong (Item cannot be found in the collection corresponding to the requested name or ordinal.) But that CAN'T be, I'm using that exact same query elsewhere, using the same (but restarted) connection.
This is the bit of code I use to instantiate the download of the file:
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "attachment; filename=overicht.xls"
There might actually being to things going on here, but I am most insterested in why IE wants to download the asp page and FF offers the right download.
The & inside onclick="" should be html-encoded to &
If fromdate contains slashes you're probably safest to url-encode that as well (though you seem to contradict that with your example URL).
Something that might help: Server.URLEncode
fromdate=<%=Server.URLEncode(fromdate)%>
But, your Excel file error -- Item cannot be found in the collection corresponding to the requested name or ordinal. -- is from Recordset.Fields(). You're trying to grab a field that isn't available -- either a column name that isn't in your query or an index that's beyond your column count.