sealz
sealz

Reputation: 5408

Download .CSV/.TXT From asp.net page without prompt

I currently have a handler that when navigated to prompts the user to download a file.

I need to programmatically disable this box and allow for the file to auto download when that page is hit. Would it be easier to read that pages text and then rewrite my own file somehow?

Which would be the best approach and how would I go about doing it.

Currently I have

Sub ProcessRequest(ByVal context as HttpContext) Implements IHTTPHandler.ProcessRequest
Dim filename as String = myfile.csv
context.Response.ContentType = "text/csv"
context.Response.WriteFile(filename)
End Sub

This currently prompts the user to choose a save location and file name etc etc before starting the download.

EDIT:

As mentioned in a below response this has to be possible. Download sites automatically start downloads all the time as soon as you navigate there.

I have come across some other sites that mention IE settings could be changed? I don't know if this could be usful in finding a solution

Super User disable Popup for download

Upvotes: 1

Views: 1158

Answers (3)

gbro3n
gbro3n

Reputation: 6967

Yes you can. You create a form on the page, hide it and have it downloaded via javascript. It might look something like below. You set up an html form, and have it submitted by Javascript when the page has loaded. I've just tested in Chrome / Firefox / IE. Think about it, those download sites you see that show a message like "Your download should begin within 5 seconds, if it does not click here...". You may want to include a direct link incase of any browser settings that may block.

Note use Response.Clear() prior to the other response code.

<h2>Index</h2>

<!-- Where /Download/GetFile will cause your code as posted to execute. -->

<form action="/Download/GetFile" id="form1" method="post"></form>

<script type="text/javascript">

    document.getElementById("form1").submit();

</script>

Upvotes: 1

Lester
Lester

Reputation: 4413

Check out jQuery.twFile. Internally it seems to use Applets or other mechanisms to save, but it might fit your needs.

Upvotes: 0

Mike Mooney
Mike Mooney

Reputation: 11989

You can't. You don't have the right to forcibly download a file to another user's machine without their permission, so the browsers deliberately prevent it. The whole reason that prompt is there is so that the user can be aware that the website is trying push something to their machine and allow them the option of allowing it.

Upvotes: 4

Related Questions