Force Download of a File on Web Server - Asp .Net C#

Force download of a file on web server - ASP .NET C#

Create a separate HTTP Handler (DownloadSqlFile.ashx):

<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.Web;

public class DownloadHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
var fileName = "myfile.sql";
var r = context.Response;
r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
r.ContentType = "text/plain";
r.WriteFile(context.Server.MapPath(fileName));
}
public bool IsReusable { get { return false; } }
}

Then make the button in your ASP.NET page navigate to DownloadSqlFile.ashx.

Force download ASP.Net

Is the file string's path actually correct?

this.Response.AddHeader("content-disposition", "attachment;filename=" + file);

Should it not be filename?

Why are you deleting the file before it is written to the response? Would it not make more sense to serve the file via the response and then delete it?

i.e. call

File.Delete(Server.MapPath(fileName));

after the repsonse.

You should try:

Response.TransmitFile( Server.MapPath(fileName) );
Response.End();

TransmitFile is very efficient because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernal cache (based on IIS's caching rules).
Response.End();

Force the Download File dialog Not Working - ASP.NET C#

In order for the download to start from a different server, you need to send a redirect answer to the client (Response.Redirect(mediaURL)).

As a consequence, you cannot force the download dialog from your web server because the browser will send a separate request to the other server. This must be solved on the server where the media is served from.

The only alternative is that you act as an intermediate, i.e. you download the media file to your server and send it as the response to the client. This shouldn't be too difficult if it's a small file that easily fits into memory. However, if it's a large file it might involve some tricky coding so you can receive and send it piecewise.

ASP.NET file download from server

You can use an HTTP Handler (.ashx) to download a file, like this:

DownloadFile.ashx:

public class DownloadFile : IHttpHandler 
{
public void ProcessRequest(HttpContext context)
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ";");
response.TransmitFile(Server.MapPath("FileDownload.csv"));
response.Flush();
response.End();
}

public bool IsReusable
{
get
{
return false;
}
}
}

Then you can call the HTTP Handler from the button click event handler, like this:

Markup:

<asp:Button ID="btnDownload" runat="server" Text="Download File" 
OnClick="btnDownload_Click"/>

Code-Behind:

protected void btnDownload_Click(object sender, EventArgs e)
{
Response.Redirect("PathToHttpHandler/DownloadFile.ashx");
}

Passing a parameter to the HTTP Handler:

You can simply append a query string variable to the Response.Redirect(), like this:

Response.Redirect("PathToHttpHandler/DownloadFile.ashx?yourVariable=yourValue");

Then in the actual handler code you can use the Request object in the HttpContext to grab the query string variable value, like this:

System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
string yourVariableValue = request.QueryString["yourVariable"];

// Use the yourVariableValue here

Note - it is common to pass a filename as a query string parameter to suggest to the user what the file actually is, in which case they can override that name value with Save As...

How can I force a links to files trigger download in the browser (and not open)

As far as I know it is not possible by just setting some special string to the href property of an <a> tag.

But you can get your desired behaviour by replacing the link by an asp.net linkbutton and a postback in which you call the following method:

public static void DownloadFile(string filename, string path)
{
Response.ContentType = "application/unknown";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
Response.TransmitFile(path);
Response.End();
}

path is the full path to the file you want to send to the client and filename is the name which the file should have when it is sent (can be different from the original name).

How to force browser to download, not view, PDF documents in ASP.NET Webforms

Use a linkbutton so you can run serverside code on click:

<asp:LinkButton ID="hl_download" runat="server" OnClick="hl_download_Click">Download</asp:LinkButton>

Then, in the codebehind:

public void hl_download_Click(Object sender, EventArgs e)
{
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Transfer-Encoding","Binary");
Response.AddHeader("Content-disposition", "attachment; filename=\"IdeaPark_ER_diagram.pdf\"");
Response.WriteFile(HttpRuntime.AppDomainAppPath + @"ideaPark\DesktopModules\ResourceModule\pdf_resources\IdeaPark_ER_diagram.pdf");
Response.End();
}

This assumes that the web path maps cleanly to the filesystem path of the file. Otherwise modify Response.WriteFile() so that it points to the location of the pdf file on the filesystem.

Force the browser to save downloaded file in a specific location

This will not be possible as it would introduce a severe security problem. A user has to decide where the file will be saved.

You can only specify a location on a server to which you have access to.

If its an internal site, then you could setup the server to save the file to a network location and return that path to the user..

If you want to show a save as, add this to your ActionResult to indicate a file download:

Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
return myFileStreamResult


Related Topics



Leave a reply



Submit