Iterate through directory filenames using JavaScript on ASP.NET C# - asp.net

I'm trying to read all filenames from a specified (server- not client) folder, and insert all the filenames into a javascript Array.
It should behave like the Directory.GetFiles method in ASP.NET C#.
I created a new array, and I just need the loop method (Javascript or jQuery) to iterate in the specific folder, and insert all the filenames into the array.
Thanks all helpers!

You're going to need to invoke some form of ajax postback to do this on the server, returning an array of strings as a JSON result.
Note that this has the usual caveats that the web server account must have the necessary permissions to be able to query the selected folder.

I wouldn't do this - it's potentially a security problem.
Out of interest, why are you doing this? I'm trying to imagine what you gain from having this information on the client...

Related

Executing a method which is named via a config file

In short: I have a method name provided via a JSON configuration file. I'd like to call a method using this provided name. The method (with a matching name) will exist in the backend. What's the best way of going about this?
I am not quite sure what I should be searching for as an example.
To detail: I am working with a legacy application, hence the VB.NET. I am building a single PDF file from multiple PDF sources. Most of these are as is, I simply read the configuration and grab the relevant files and the job is done. However some require processing, I'd like the configuration file to pass in a method name to be called that will perform extra processing on the PDF, whatever that may be.
As there can be a lot of PDF files that can vary, I cannot simply use a property such as "PostProcessing: true".
Any ideas?
You could use reflection to reflect method names back and check them against the name passed from the property in the config file.
Like so
Type magicType = Type.GetType("MagicClass");
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
That would work.. but to be honest, I'd go with a case statement as you'll be hardcoding the method names anyway (because they are code), and it'll be strongly typed (less chance of typos and errors).

RazorEngine output HTML

I have a little piece of code, which gets data from a DB, and I want to parse it into a .cshtml Razor-View-Template. therefor i create a variable of a new TemplateService and execute .parse() on that. Now I want to output the file to the user. What is the best performing function to accomplish this task? I have an MVC application. And as the text above already describes, this is a little template system.
Edit:
For example:
I have a folder templates/default-tpl/, which contains a file index.cshtml
This file contains some Razor-related variables (for example #ViewData["Title"]
Now i have a Controller, which calls the RazorEngine TemplateService's Parse() function with the corresponding ViewData.
Now I have the HTML ready and parsed
So the question is, from this point, how do i output the website the most performant way? Or is there even a complete different approach possible?

How to override resource (.resx) behaviour

We are applying localization in our application by using resx files and using it by calling Resources.Resource.Key and ResourceManager class to get the values of keys. Currently we are facing an issue that in some languages single (') and double quotes (") are appearing while in English resource there is no such thing like that. Problem is that when we calls javascript methods like alert('value') in code then it crashes because single quote within another single quote does not work. I know there is way to handle it by replacing single quote with "\'" but in order to fix this I need to write this code throughout the application. Is there any workaround that whenever I call the resource by calling above ways I mentioned earlier One method automatically called in which I can modify the value return by the resource. Waiting for your valuable suggestions. Thx
Anywhere you're referencing resources you should HTML encode the output. If you're using ASP.NET WebForms you can use <%: Resources.Strings.Something %>. If you're using the Razor view engine then it will be HTML encoded by default.

SWFUpload multiple files server-side handling

I need the user to be able to upload multiple files to my server, thus I am using the SWFUpload utility. SWFUpload sends the files one by one, and I need to store them all in the same temporary directory. My ASP.NET handler recieves the files one by one and I can store the file appropriately.
My problem is: How do I know which files belong to the same upload? Rephrased, how do I connect the files in my handler?
I see 2 ways to do this (but they inherit the same idea).
The idea is based on the thing you should have something like sessionId parameter. This value should be unique for each set of files. You could use javascript uuid generators or something like so.
Further, this sessionId variable may be passed through query string(a little bit modified your handler url): ~/UploadHandler.ashx?sessionId={whatever} or as post parameter(guess, the better way).
The value may be retrieved on the server side by using: context.Request["SessionId"] for example.
On the client side you should be able to change post parameters or handler url dynamically. It could be done by using:
void addPostParam(name, value)
The addPostParam function adds a
name/value pair that will be sent in
the POST for all files uploaded.
The name/value pair will also appear
in the post_params setting.
or
void setUploadURL(url)
Dynamically modifies the upload_url
setting.
client methods. They should be called from the
fileDialogComplete(number of files selected, number of files queued, total number of files in the queued)
The fileDialogComplete event fires
after the File Selection Dialog window
has been closed and all the selected
files have been processed. The 'number
of files queued' argument indicates
the number of files that were queued
from the dialog selection (as opposed
to the number of files in the queue).
method.
Hope, this helps.

How to maintain the cache in the servlet?

I want to maintain db cache(some keywords) in the servlet. When I am typing for 'a' I have 1000 keywords in Db which starts with 'a' and presently I am using js file to store all the keywords in cache. I want to maintain the DB cache in servlet also and decrease the browser cache and next hitting keyword matches in the servlet Db, I want to retrieve the top 10 keywords for the this hitting.
Can you tell me how can I create the servlet cache? Can you provide any pseudo code for that?
Thanks,
Murali
I can imagine you have a Servlet that accesses the Database in order to retrieve the top 10 keyboards based on the input delivered. That means whenever an A is pressed in the input field, you must use an XMLHttpRequest to call the servlet with that input.
The servlet should return you a list of keywords which you should parse and translate properly to your user again. (you could do this in multiple ways. An easy way is to just let the servlet respond with HTML for you,which you can set with Javascript in an element (innerHTML)).
As for caching, the servlet could use some cache and identify the requested input. You can build an own cache by generating a key from the input and the result of that input should be put into a Map.
You could also use an existing caching framework, like EHCache.

Resources