1

before I start digging could you please tell me if it is possible to do the following with webmatrix and c#.

At the moment I have a plain html website with many <a href links. Each link once clicked triggers a "shortcut file" on the server which then opens up an external program. This works because it's an Intranet website and the program is installed on every computer.

The "shortcut file" basically is a .txt file (just a different ending) with search parameters that will be executed inside the external program once it has been started.

This means I have to create a shortcut file for each link and save those files on my disk. At the moment this is still maintainable but already annoying. Especially when all I need to change is one parameter (search string).

So I am wondering if it is possible to create this shortcut file "on the fly" and have it pop-up the "open with..." or download dialog of a web browser.

Since I usually only have to change the search string I could then place the string in my a element, for example the "title" attribute and take this value to trigger the search process once clicked.

<a href="search/linktomyfile.end" title="mySearchParameter">Additional Text</a>

Here's an example of such a file:

search
searchtext_val=
doc_type=mySearchParameter
text=
cat=hardcoded
subcat=hardcoded

In this example I would only need to deal with the doc_type as a parameter, everything else is either empty or predefined.

I'm sure it is possible to build this but I can only hope it's a task I can handle.

Hopefully you can point me to links or even provide a little sample for demo purposes.

Thank you in advance,

Tony

Tony Clifton
  • 703
  • 3
  • 14
  • 27
  • Are you talking about "web pages", "web forms", or "MVC"? Honestly, though, provided I understand your question correctly, it should be possible to do what you ask in any of those environments. I could probably help out with a lot, but probably not everything. – VoidKing Aug 07 '13 at 13:26
  • I know that C# is capable of making files on the fly, but I've never made a shortcut file before (am I understanding this correctly)? I can tell you this, though `File shortcuts (also known as shell links) were introduced in Windows 95. Microsoft Windows uses .lnk as the filename extension for shortcuts to local files, and .URL for shortcuts to remote files, like web pages.` Now, how you would edit such a file is another story, but I'm afraid that I'm at a loss there.... – VoidKing Aug 07 '13 at 13:29
  • Ah, forgive me, I didn't see your `asp.net-webpages` tag. Sorry about that. I do have a bit of experience with webpages (as opposed to the other two options). Surely I could be of some help here (unless you've done a lot of C#.netting yourself), but your question should be narrowed. Start off attempting this and ask questions along the way when you've exhausted all other options of finding a solution. – VoidKing Aug 07 '13 at 13:37
  • yes, a solution for webpages is needed. I've just started with asp.net using webmatrix so I could definitely need some help to get me going with the overall process. – Tony Clifton Aug 08 '13 at 05:22
  • with jquery I can get the clicked title attribute like so: `$(document).ready(function() { $(".testClick").click(function() { alert($(this).attr("title")); }); }); ` HTML: `Test1.` but how can I use this within webpages? – Tony Clifton Aug 08 '13 at 05:55
  • I'm able to manually execute the shortcut file and have it open the appropriate program with: `@System.Diagnostics.Process.Start(myFile);` – Tony Clifton Aug 08 '13 at 06:09

2 Answers2

2

Perhaps you are hung up on how to actually write to a file in C#?

List<String> lineList = new List<String>();
lineList.Add("search");
lineList.Add("searchtext_val=");
lineList.Add("doc_type=customSearchParameter");
lineList.Add("text=");
lineList.Add("cat=hardcoded");
lineList.Add("subcat=hardcoded");

Then you need to make the file (this example does not name the file using GUID, but depending on your situation, you may need this if you have multiple users running this program at the same time).

System.IO.File.Delete(Server.MapPath("~/folderInRootOfSite/example.txt")); //This will delete the file if found (in case there is a lingering old version of this file still out there). This will not throw an error if the file is not there, but will delete it if it is.
var outputFile = System.IO.File.AppendText(Server.MapPath("~/folderInRootOfSite/example.txt"));
foreach(var lineVal in lineList)
{
    outputFile.WriteLine(lineVal);
}
outputFile.Close(); //This line is so necessary that if you forget to include it, the process will not close and will even prevent some access to the file and will even prevent its deletion (saying something to the effect of "this file is open in another program, etc., etc., etc.").
//Now the file is saved in "~/folderInRootOfSite/example.txt".
//This is where you would have to either utilize this file from the server or perhaps write an html page and utilize JavaScript on the client-side, however, I'm not sure what functionality, if any, you will be able to use from the client-side (especially without ActiveX).
//Once you are done with whatever, don't forget to delete the file if you want it cleaned up after this process.
System.IO.File.Delete(Server.MapPath("~/folderInRootOfSite/example.txt"));

Then, if you do want to execute it with the default associated program from the server (the server will be the machine that runs it), use:

string filePath = Server.MapPath("~/folderInRootOfSite/example.txt");
System.Diagnostics.Process.Start(filePath);

However, I find it doubtful that this will really be what you want. I have heard that you can "upload a file to the browser" (I'm not sure if it was worded right, when I read it, but...) but I am not sure if you can do this, how to do this, or where any good documentation is on this.

I realize that this probably won't totally solve your issue, but I will be happy to append more to this answer as you inform me of whatever ever else may be hanging you up.

----------UPDATED INFORMATION--------------

In order to use the download manager of the browser for your files, all you need to do is to path to said file in a regular <a> tag, like this:

<a href="/fileDirectory/file.doc">Click here for file download!</a>

Which will normally work fine, however, there is still one more thing to consider. Most browsers have built in viewers for certain types of files (.pdfs are a good example), which will, instead of using the download manager, simply open up the file to be viewed within the browser.

There are a few ways to force the download manager, however, which I will share with you from the following site: http://www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the-browser

How Browsers Work With File Downloads Usually when a user goes to a file URL (for example: a download link), the file will show in the browser if the browser supports it. Image files like jpg, png, gif etc. will almost always show in the browser. Archive files like zip, tar, gzip etc. will always be downloaded. Some file types show up in some browsers but not others depending of if the browser can read the file or not. For example, Internet Explorer (IE) will usually try to show Microsoft Word files (doc and docx) in the browser, while most other browsers will download it. Google Chrome has its own PDF converter and it will try to convert a PDF file and render it in the browser.

The key thing to understand is that some browsers maybe able to read a particular file type based on the addons you have installed for that browser while others may not be able to. If a browser can read the file type it will show it in the browser. If the browser cannot read a file type it will force a download to the hard disk. Usually this is not an issue since the users can save the file to there computer after it is shown in the browser.

How to Force a File Download For All My Files Some users prefer that all the files should be forced downloaded (no showing in the browser). There are a few things you can do to ensure that.

Option 1: The easiest solution is to put your file (example: a PDF file, a movie file) inside a zip file if possible (If you are using the PDF Stamper plugin then you can't do this unfortunately). Browsers cannot read zip file so it will force a download. Most computer users know to unzip the file and get the content from inside.

Option 2: Most browsers have a settings where it lets the users specify if they want to force download a certain type of files (for example: a pdf file). You can instruct your users to use this option if you think it will help them (again this is common knowledge and most internet users know this).

Option 3: You can add the following lines of code in your .htaccess file of your server to force a download of a particular file type from your server (in this example it is for PDF file types):

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

You can change "pdf" to any other file type based on your need (example: doc).

Now, that having been said, it seems that in your example you used some kind of custom file extension (I think), which would, of course, not be recognized by any browser, so you may not need to do anything extra after simply setting up the <a> tag. This answer, however, seems almost too easy, so I'm not sure if this is actually what you're after or not.

So, if I understand your situation correctly, you should be able to set up such a tag, and after it is downloaded by your user, check where it was downloaded to and execute such a file with C# (you might need AJAX at some point during this process, but I'm not really sure without delving deeper. I am also not sure, since the user will have control over where on their hard-drive the file gets saved, how to check where it was saved for future use with System.Diagnostics.Process.Start(filePath);)

One possible location for such information involves simply detecting when the file has been downloaded, but seems pretty complex, and, tbh, I'm not sure I am quite at the level to fully understand all of this, but here is a link anyway: Detect when browser receives file download

Community
  • 1
  • 1
VoidKing
  • 6,282
  • 9
  • 49
  • 81
0

You can certainly generate the series of links dynamically using Web Pages. One approach is to use a database or XML and store what varies in a structured format. Then you can query the store and loop through the contents, outputting links as you go:

foreach(var item in result){
    <a href="search/@item.Filename" title="@item.SearchParameter">@item.Text</a>
}

This example assumes that Filename, SearchParameter and Text are the elements that might vary from link to link.

For more information on the basics behind managing data in ASP.NET Web Page, read this tutorial: http://www.asp.net/web-pages/tutorials/data/5-working-with-data

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • I am aware of looping through the elements - what I'm looking for is a way to create above file structure once the link has been clicked which then should trigger the external program. I'm guessing I need javascript for this plus a c# function. – Tony Clifton Aug 08 '13 at 09:46
  • C# executes on the web server. If you want to create files on the client, you can only do this in IE using ActiveX. – Mike Brind Aug 08 '13 at 10:42
  • I don't mind creating the file on the server as long as it can trigger the external program. It's meant to be a temp file anyway. To clarify my thoughts on how I think the process should work: user clicks on link -> file is created in the bkg -> same file opens up in the browser -> external program is started – Tony Clifton Aug 08 '13 at 11:21
  • @TonyClifton What's the connection between this temporary file and the external program? I mean, forgive me if I don't understand, but I don't get why you need this file to begin with. Is it a txt file that contains info you need for the program or are you just using it as some sort of trigger? – VoidKing Aug 08 '13 at 13:33
  • @VoidKing This plain text file with the added extension of the external program performs a query within the program. So if I'd take above example of such a file and save it with the appropriate extension I could double-click it which would then open up the program (which is always running in the bg) and within the program I then get a result list of the executed query. So basically this file + its extension is the only way to trigger the program to perform a query. And instead of managing those files on the server I think it would be better to only create them when they are needed. – Tony Clifton Aug 09 '13 at 06:00
  • @TonyClifton Ok, I see :) Well, I know you shouldn't have much of a problem creating the file on the fly. Server side languages (I prefer C#, myself, but then again, I know that language better than others) can definitely be used to create files, then when done with them, simply delete them with the same language. So, I don't think that part will be a problem (let me know if you would like some example code, but you seem pretty knowledgeable, so I'm not fully convinced that you don't know how to do this part, already), but the "shortcut files" you refer to, they are `.txt` files? – VoidKing Aug 09 '13 at 14:52
  • @VoidKing could you please provide some sample code? I'm especially looking for the part where the user clicks on the href link to get the whole process started. For testing you can simply use .txt files. Thank you. – Tony Clifton Aug 12 '13 at 11:37
  • @TonyClifton I will do my best to try to add what help I can, but I just want to reiterate what Mike said in that you are gonna want to create this file on the server-side (probably using GUID, depending on how many users you will have clicking about, out there) unless you want to limit all of your users to IE's ActiveX only. That having been said, this server-side approach may not even be possible depending on what you mean by "file opens up in the browser." I mean you can use AJAX to make a round trip to the server, but I'm not sure that the client side browser will be able to utilize a... – VoidKing Aug 12 '13 at 14:13
  • @TonyClifton ...file stored on the server. – VoidKing Aug 12 '13 at 14:13
  • @TonyClifton I can tell you that `System.Diagnostics.Process.Start(filePath);` is a quick and easy way to execute a file, however this will not let you have control over what program executes the file, and will, in fact, be executed by the server and not the client, so I don't know if that will help you or not. Also, don't forget that you may (I've never used this method before, so I don't know, but...) have to use `Server.MapPath("~/relativeFolderPath/exampleFile.txt")` within the `.Start()` method to find the file. Again, though, I'm not sure if this will help you or not. – VoidKing Aug 12 '13 at 14:35