Download a file on the client macine using asp.net - asp.net

I want to download a text file from my website to the users pc without prompting him for the location to save the file.
I have tried it using code below :
Response.TransmitFile("G:\Medical Reporting\Medical\Users\Vishal\Uploaded\Key.txt")
Response.End()
But every time I am just redirected to the new page and all the contents of the file is written there. I don't want to display the contents of the file, but I want to download the file.

Not possible - it's a security issue, otherwise the world would be trying to save all sorts of files on a users machine.
If it's an intranet each user could have a shared drive on a network accessible to the web app and simply copy the file using IO.File.Copy method.
Update
To Prompt a user to download a file you can use the following code which will be fired after clicking something like a button:
this example is for an image, though you can just change the ContentType filename to suit your needs.
Response.ContentType = "image/jpeg";
// this is the important bit that gives the user the prompt to save
Response.AppendHeader("Content-Disposition","attachment; filename=yourfile.jpg");
Response.TransmitFile(Server.MapPath("~/yourfile.jpg"));
Response.End();

Related

Displaying Save File Dialog in Asp.net web page

I have a ASP.net page which writes a file to the local disk.
I want to present the user a Save File dialog box and allow him to set the path to the folder.
I know code like below can be used;
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader( "Content-Disposition", "attachment;filename=\"report.csv\"" );
// write your CSV data to Response.OutputStream here
Response.End();
But it fixes filepath.
I need to capture the filepath that the user selects.
Is that possible in ASP.net?
Thanks.
it does not work like that from a web page, you have to initiate the download suggestiong a target file name then the user can override your suggested file name and select any folder or filename he likes and your content will be saved in that location.
you do nothing with a local path which only makes sense on the client machine on the server side of ASP.NET application.
I need to capture the filepath that the user selects. Is that possible
No. Your web server presents a file to the client, where the client has the option to save this file.
In what way would the path the client saves this file be interesting to the server?

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!!
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.
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.

Asp.net - Prevent file (PDF, Word) download from a particular folder

I am creating a recruitment site and have a folder called /CV/ where I am storing resume files uploaded by the member.
Lets say a user saves their resume and its called 123.pdf and is stored in cv/123.pdf.
How can I prevent the pdf file from loading in the browser window or downloading to the users machine if they type in 'http://mydomain.com/cv/123.pdf'?
I am using forms Authentication, Asp.Net Membership and Roles Providers, Asp.net 4 on an IIS6 server.
Create a folder that is outside of the hierarchy of the main www folder used by the site (so it cannot be directly accessed through url)
Use an ashx handler to provide access to download the file. The logic within the ashx file can validate whether the user is authorized to download the file or not.
ASHX references: 1, 2, 3
The best way would be to put the files somewhere else, and write some code to access them -- then that code can verify whether the caller has the necessary rights.
For instance, you may store files in your /uploads/xyz123/ directory. Then in order to download a file, say myresume.pdf, the user would have to surf to http://yourserver/download.aspx?file=myresume.pdf.
That page then does the necessary validations, loads the file and outputs it as a binary to the browser, like so:
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.AddHeader("content-length", binaryStream.Length.ToString);
Response.BinaryWrite(binaryStream.ToArray());
Response.Flush();
Response.End();
No user will ever find out where the files are actually stored.
You can simple save the file in a directory that is not part of your web application.
If you want to store a file that should not be reached via http, do it this way.

How to get a file from a Database and put it in an FTP?

I have an aspx page that gets a list of documents available in a database through a DataGridview and I want that when the users clicks on the link of the document he want's to sent it as an FTP or HTTP document, like if it was on a drive on the server.
The problema I have is that I know that when the file is on a drive it is easy but if it is on the database I do not know how to serve it to the web page user.
I thougth maybe that when de user clicks on the link to save the file to a temp directory, and then redirect the page to that page with the name of the file, but I do not know if it is to much of a touble and there is a better way. Of course if I do this I will have to delete the file from the drive after it has been downloaded.
I get the way to do this, is just a matter of use
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");
//Response.BinaryWrite(foto);
Response.OutputStream.Write(foto, 0, foto.Length);
The file, is in binary.

Deleting Application Temp File from ASP.Net

I have a WebPage where I am giving the option to to Export the Form data to PDF. I am creating the PDF at run time and store the PDF in a "PDF" folder which is under my application directory. After creating the PDF with the SessionID name I Call following function to show the PDF file in the new browser window:
ResponseHelper.Redirect(Response, "~/PDF/" + Session.SessionID + ".pdf", "_Blank", "");
This PDF contains the private information related to the logged in user. Therefore, I want a way to delete this PDF file once it is shown in the browser to the user. This is because the IIS server allows whole development team to view this folder which is a security risk, and we can't disallow user to view this folder on the server.
Therefore, if I could delete this file as soon as it is loaded in the browser could be a solution of this security risk.
Can anyone suggest some better ways of deleting this file as soon as possbile from the application?
Thanks,
Praveen
what i guess is you are creating PDF file on runtime using Itext and then you save that PDF file in temp directory to show it to user... why don't you use
Response.WriteFile(PDFFILE);
this will write the whole file on the stream without saving it in temp folder.
One way is to write an ashx handler which streams the pdf to the browser, then deletes it when done.
Another, and much better way, is to simply build the PDF in memory (NOT using session) and stream it as soon as it's ready.
UPDATE
I'm doing this with a slightly modified version of iTextSharp. Basically, iTextSharp performed all of it's operations in memory, then saved the file to disk. I changed this to return the memory stream. All the code is already there, it was really just a line or two that had to change.
Then, I used a response.binarywrite to push the stream directly to the browser. viola! no files on disk.
An ashx handler is just like an aspx page, only it has one entry point and doesn't do all of the page processing garbage. It's light weight and communicates back to the browser by response.write calls.

Resources