Ehud Grand
Ehud Grand

Reputation: 3693

Download and run file in client machine using asp.net

I'm trying to download and run a file to the client machine. The client is aware of that. It's a ttkgp file that's dynamicly generated. I've tried using Processs.Start() that worked fine on my local machine (first saved the file to C:\ then lunched it), but it's not working from the server. It's not my server but a hosted one. They are trying to help but no luck so far. I've seen this code:

  public void ProcessRequest(HttpContext context)
  {
   string fileName = context.Request.QueryString["filename"];
   FileInfo fi = new FileInfo(fileName);
   context.Response.ContentType = "application/x-rar-compressed";

   context.Response.AppendHeader("Content-Disposition",
   string.Format("attachment; filename=download{0}", fi.Name));

   context.Response.WriteFile(fileName);
   context.Response.End();

}

But since I dont know what's "HttpContext context" is, I've no idea if it works. Is it some server previlges need to be changed? or simply this code will do the trick?

Thank you

UPDATE (24.6.12): I'm nearly finished with the problem, all I need now is to know how to open an html page in a new tab / window and close it second later. Once I'm done, I'll post back here all the process, I'm sure it'll help other people.

UPDATE (26.6.12): Here's what I've got: The goal is to download an TTKGP file from asp.net webiste to local user machine and run it. Step 1: generate the file with code behaind (c#) on the server (V) Step 2: copy the file or it's content to user machine (X) Step 3: run the file using JS (V)

Here's the thing: I CAN copy from a text file on the server to a text file on the user machine, but not from TTKGP files. It's strange because this are just text files just a different extantion. The code for copying text files:

enter code here
    function copyremotetxt() // works
{   
// copy the txt file
var fso = new ActiveXObject("Scripting.FileSystemObject");

var newfile = fso.CopyFile("remote.txt", "C:\\Users\\***\\local.txt");
}

Perhaps I can change the file type on the user machine?

Notice 1: I know that's a security issue, the site is just for known user not the open public Notice 2: I know there are better ways to get the task done, but there are strict limitaions on many things

Thanks for those how can help!!

Upvotes: 0

Views: 3861

Answers (2)

Ehud Grand
Ehud Grand

Reputation: 3693

Ok, this need a different aproach. I'll try using JavaScript do read the file on the server, rewrite it in the user machine and activate it. Any clues would be grate! For a start, how to I read file in JS? I'm new to it.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

This code will do the trick. It will prompt the client to download and save the file on his computer at the location he decides. What happens next with this file is the client's decision, not yours. He might simply close the Save As dialog, interrupt the download, delete the file, ... It's up to him.

Another remark: this code is extremely dangerous because it allows the client to pass any filename he wants as query string parameter and download it. So he could read absolutely all files on the server which is probably not something that you want to happen.

Upvotes: 2

Related Questions