There is one URL with specific syntax to download a file.
http://www.hddownloader.com/?url=http://www.youtube.com/watch?v=N-HbxNtY1g8&feature=featured&dldtype=128
The user enters the file name in the textbox and presses the download button.In the click event the Response.WriteFile is called which sends the file to the client.
Now I want to create another website with a page, in which the user enters the filename and presses the download button to download that file.
Now I want to utilise the first URL for that. I dont want to use Response.Redirect, because by that way, the user will come to know that I am using mydownload.com.
How can I acheieve that.
One way is : When we download something from microsoft's website, a small popup window(with no close, maximise and minimise button) and then the save dialog box appears.
How to achieve this or another to achieve the same ?
You could first download the file from the remote location on your server using WebClient.DownloadFile:
using (var client = new WebClient())
{
client.DownloadFile("http://remotedomain.com/somefile.pdf", "somefile.pdf");
Response.WriteFile("somefile.pdf");
}
or if you don't want to save the file temporary to the disk you could use the DownloadData method and then stream the buffer into the response.
UPDATE:
Example with the second method:
using (var client = new WebClient())
{
var buffer = client.DownloadData("http://remotedomain.com/somefile.pdf");
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment;filename=somefile.pdf");
Response.Clear();
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
}
Related
I require some assistance, I have a pdf file that is located on a ftp location.
What i want to do, is on a button click i want to retrieve the pdf file and display it on a new tab.
Please assist on how to accomplish this task.
Thanks in advance.
One approach would be to make a web request to download the file and then pass the download stream along to the ASP.NET response stream. I haven't compiled or executed this code, but it might look something like this:
WebRequest request = HttpWebRequest.Create("ftp://ftp.yoursite.com/yourfile.pdf");
request.Credentials = new NetworkCredential("ftpuser","ftppassword");
// Get the response
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
// Send the response directly to output
Response.ContentEncoding = responseStream.CurrentEncoding;
Response.ContentType = "application/pdf";
Response.Write(responseStream.ReadToEnd());
Response.End();
Just use the HTML anchor tag ("a" tag). In the href attribute put the FTP address of the PDF file (eg. ftp://example.com/file.pdf). To open in a new window you should also specify a target attribute with the value "_blank".
Example:
<a href='ftp://example.com/file.pdf' target='_blank'>Click here to open the PDF file</a>
I am working on an Application based on Monorails which generates images / charts at runtime. The code writes the images to OutputStreams and not to the Appserver hard drive. It allows the user to download the image. I want to make sure that only the logged in user is able to download the image and no one else.
For e.g if any user A tries to download image generated for user B user A should not be able to do that no matter what.
Note: I am not storing the file to the hard disk.
How can I implement this???
Below is the sample code I have written for the image download.
var stream = new MemoryStream();
Response.Clear();
Response.ContentType = "application/image";
Response.AddHeader("Content-Disposition","attachment; filename=" + imagefilename);
var array = stream.To Array();
Response. OutputStream.Write(array,0,array.length);
Response.End();
Please help me fix this issue.
Thanks,
Rahul
You need logic in the code to check if the user logged in 'owns' the image they are trying to download.
If they don't 'own' it, Response.End their request
This is my first time writing code that allows a user to download a file uploaded by another user.
I've written an ASHX file, download.ashx, with code that looks like this:
s = context.Request.QueryString.ToString();
byte[] buffer = new ReplacementTicketFileIO().GetSpecifiedFile(s);
context.Response.BinaryWrite(buffer);
context.Response.Flush();
context.Response.End();
When a user clicks on a link to download.ashx with the appropriate querystring, the file is downloaded, but the browser wants to display the content in the browser window. If the user right-clicks on the link, he can download the file, but the name of the file defaults to download.ashx.
I would like to accomplish two things:
1) I would like to be able to specify the default name of the file downloaded on the user's device based on the querystring.
For instance, if the user clicks on download.ashx?linkedfile=car.pdf, I would like for the browser to default to car.pdf for the name of this file.
2) I would like for the browser to default to saving the link, as opposed to opening the link in the browser window.
Is it reasonable for me to want to do this, or is there a better way to download files? Please let me know.
Set the Content-Disposition HTTP header. E.g.
Content-Disposition: attachment; filename=hello.jpg
You can do that in C# using:
Response.AddHeader("Content-Disposition", "attachment; filename=hello.jpg");
Here is something I have for excel files and I believe it forces a download rather than a new window. There is a page property for QueryString. You would just need to capture the QueryString and use it in this code as well as determining the content type. The String.Format will give you clean code.
private string _ExcelFilename
{
get
{
return (Request.QueryString["xls"] != null) ? Request.QueryString"xls"] : "bis";
}
}
Page.Response.Clear();
Page.EnableViewState = false;
Page.Response.Clear();
Page.Response.ContentType = "application/vnd.ms-excel";
Page.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}_{1}.xls", _ExcelFilename, DateTime.Now.ToString("yyyyMMdd")));
Page.Response.Write(excel);
Page.Response.Flush();
Page.Response.End();
I have a page which contains a button that perform the following code, I want to reload the page after the response end so that update a grid in that page. or suggest a way to update the grid after response end.
string report = new BLL.OrderReport.OrderReport().GenerateFullfilmentReport(fromdate, toDate
, string.IsNullOrEmpty(generationId)? null : ((int?)int.Parse(generationId)) );
if (!string.IsNullOrEmpty(report))
{
LoadReportHistory();
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=Report.text");
Response.Write(report);
Response.End();
}
Thanks
I work around this, the problem is that I need the Grid to be rebind after Response.End is called.
The target is updating the Grid when user click download. To work this out I rebind the grid first, and register a java script code to cause reloading the page (post back event will be resent),
in the second round I download the file.
I control this miss by copying the original file to temp file, then delete the temp file during first round, if the file exist then download.
Someone tell me something better please
I have a directory with a lot of files in it named: file001.pdf, file002.pdf etc.
Now I want a linkbutton in asp.net witch opens one of the above files with an other name!
So for example the linkbutton show's and opens when clicked the pdf: newname.pdf.
Is this possible in asp.net? I only want the client to see the newname.pdf and I want the file001.pdf remains on the server.
You could try reading in the file into a Byte array (or as a stream) and then output the file with a different name?
Byte[] myFile = System.IO.File.ReadAllBytes("~/MyPdf.pdf");
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "filename=NEW_NAME.pdf");
Response.OutputStream.Write(myFile, 0, myFile.Length);
Response.Flush();
Note, not tested this. But it should point you in the right direction...