How to force file download on Telerik RadEditor? - c#

I am using Document Manager in Telerik RadEditor.
Once I upload .txt file and click on that link, it opens on the browser instead of downloading it. How to force download that file without going in to .htaccess or other server changes?

Short answer is: using RadEditor alone you can't.
In order to make a browser-viewable file type be served as a download you must send it to the client's browser with a 'Content-Disposition' type of 'attachment'. Doing so is fairly simple, however it requires server-side code that would be outside of the scope of RadEditor.
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/path/to/file.txt"));
Response.AddHeader("Content-Type", "text/plain");
Response.AddHeader("Content-Displosition", "attachment;filename=file.txt;size=" + bytes.Length);
Response.Flush();
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
Unless you want to write a specific handler for serving the file in question, your only option is to instruct users that they must 'Right-Click > Save Link As...' on your text file link.

Related

How to redirect to another page after csv file download?

I have download button which after download i want to redirect to another page.
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=downloadfile.pdf");
Response.TransmitFile(Server.MapPath("downloadfile.pdf"));
Response.AddHeader("Refresh", "3;URL=redirectpage");
This is the code for downloading the file, right now after downloading the file is not redirecting to the page i want to redirect.
Depending on how the request for the file is made, you may want to consider using some client-side code to perform the redirection. A small example of a file download requested from a hyperlink can can be found here:
https://social.msdn.microsoft.com/Forums/sharepoint/en-US/58ff5ed5-c3af-40f9-b136-b0415a0c767c/redirect-after-file-download?forum=sharepointdevelopmentprevious
This example redirects after starting the file download.
You could also redirect then initiate the download as show here (context is with PHP but solution is HTML-based and JS-based)
PHP generate file for download then redirect

File Download in C# onClick Method through Ajax

SO I have been searching a lot but only found Web Methods of downloading a file from a server or a URL.
My Question is that I have a desktop path of a file (pdf) and I made a table , and on its on click function I call Ajax and on back end I want to download the file from the Desktop. Any body has any idea to give me some pointers?
The file will be on the server in production environment. you can simply use Response.Redirect(Server.Mappath("~/filepath")).
it should prompt an open/save dialog on client browser.
or you can define the content type and use Transmit File.
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.pdf");
Response.TransmitFile(Server.MapPath("~/filepath.pdf"));
Response.End();

Use Response.Redirect to open pdf in browser

How to dynamically open file resource in browser using Response call in C# and ASP.NET?
I have a file server that stores documentation. I have another server that runs IIS and ASP.NET application. I need to open a pdf document stored on the file server in a browser window.
I supply folderPath string as "\\MyFileServer\documentFolder\" and filename string as "doc.pdf". I try to redirect to
Response.Redirect(folderPath + filename, true);
but redirection tries to append my provided location of the resource to current server path. I'm new to ASP.NET so please forgive me my ignorance.
Oh, BTW, the site is accessed from android tablets so I need explicitly redirect client browser to a file resource, otherwise android will start downloading the file instead of opening it.
In a response, write a Header:
Content-Disposition: inline; doc.pdf
See more here
Have a look at Response.AddHeader
You can do something like
Response.AddHeader("content-disposition", "inline; filename=" + filepath + ".pdf");

asp.net C# download file in ashx from external link

need some help in downloading file with ashx
i'm trying to download large file (about 2-4GB) from external link (file not stored on webserver)
here is my code
context.Response.Clear();
context.Response.ContentType = "video/mp4";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
context.Response.Write("http://otherserver/file.m4v");
context.Response.Flush();
context.Response.Close();
and downloaded file is 1kb
what i;m doing wrong?
and is other way to download file?
I'm trying to force browser to download file (and change filename) not to preview in brower
P.S sory for my english ;)
This is an incorrect approach. The file content will be:
http://otherserver/file.m4v
Which you are setting here:
context.Response.Write("http://otherserver/file.m4v");
What you need to use is the HttpWebRequest Class.
All you're doing is sending the browser a file containing the text "http://otherserver/file.m4v", with a header suggesting that the browser offers to download, rather than display, the file.
There's no magic going on in the browser that causes it to say "Oh, I should download whatever's at that URL" when it sees a file with a URL in it.
Moreover, having Googled around a bit and looked at several PHP discussions on this subject, I don't think there's a way to do what you want without literally streaming the file from the remote URL onto your server, and then sending it on from your server to the client.
You could try adding the header and then sending a redirect to the client, but I'd expect the client to discard the header when it makes the request to the remote URL - and so display the result in the browser.

Downloading files in an Android 3.0 browser from my ASP.NET site

I'm having an issue downloading a PDF file from my custom asp.net site in the out of the box browser on an Android tablet (a Motorola Xoom).
I'm using Response.OutputStream.Write to send the file because it's having to read the file from a UNC path. This method works on IE, Firefox, Safari, and on iPad, but doesn't work on the browser on the Xoom. However, when I downloaded Firefox on the Xoom I was able to download the file just fine.
I've found a couple of spots suggesting I use the following headers:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="MyFileName.PDF"
I've tried this with no luck. I also tried using Content-Types of application/pdf and application/force-download and every combination of filename capitalized, extension capitalized, extension lowercase, double quotes, single quotes, no quotes, etc... for Content-Disposition and have yet to find anything that works.
I have also found that if I execute the code to download the file on Page_Load it is able to download, but if I do a postback and then execute the code (clicking on a link button to download the file) it doesn't work
Has anyone been able to download a file using custom C# code on the OOTB Android 3.0 browser?
This is old but in case anyone comes across this issue:
Check unknown sources under settings --> applications.
Make sure you have PDF defined in MIME Types (application/pdf) on the IIS server.
Also if you are using a file handler you will need to set the header info.
This code block is an example of what would be used in a file handler:
string dirpath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
string file = System.Configuration.ConfigurationManager.AppSettings["FileName"];
System.IO.FileInfo fi = new System.IO.FileInfo(dirpath + file);
string contentlen = fi.Length.ToString();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + file);
context.Response.AddHeader("content-length", contentlen);
context.Response.ContentType = "application/pdf";
try
{
context.Response.WriteFile(dirpath + file);
}
catch (Exception)
{
context.Response.Redirect("Error.aspx");
}
I have no idea as to why this is happening, but if you can download a file inside the Page_Load block, the a workaround would be linking to a dedicated page for downloading where you call the download function inside the Page_Load block.
To make this more compliant with the rest of your website (if needed) you might pass different parameters to the page for downloading different files, or redirect to this page only if you detect the site is running on the android browser.
I will add code examples shortly.

Categories