Unicode in Content-Disposition header

I had similar problem. You have to use HttpUtility.UrlEncode or Server.UrlEncode to encode filename. Also I remember firefox didn’t need it. Moreoverit ruined filename when it’s url-encoded. My code: // IE needs url encoding, FF doesn’t support it, Google Chrome doesn’t care if (Request.Browser.IsBrowser (“IE”)) { fileName = Server.UrlEncode(fileName); } Response.Clear (); Response.AddHeader (“content-disposition”, String.Format … Read more

Get Content-Disposition parameters

If you are working with .NET 4.5 or later, consider using the System.Net.Mime.ContentDisposition class: string cpString = wc.ResponseHeaders[“Content-Disposition”]; ContentDisposition contentDisposition = new ContentDisposition(cpString); string filename = contentDisposition.FileName; StringDictionary parameters = contentDisposition.Parameters; // You have got parameters now Edit: otherwise, you need to parse Content-Disposition header according to it’s specification. Here is a simple class that … Read more

Force download of ‘data:text/plain’ URL

As of now, it has been made possible to use <a download> in Chrome. Using dispatchEvent, you can download any string as file (even with a custom filename) whenever you want. Here’s a utility function to use it: var downloadFile = function(filename, content) { var blob = new Blob([content]); var evt = document.createEvent(“HTMLEvents”); evt.initEvent(“click”); $(“<a>”, … Read more

How to get file name from content-disposition

Here is how I used it sometime back. I’m assuming you are providing the attachment as a server response. I set the response header like this from my REST service response.setHeader(“Content-Disposition”, “attachment;filename=XYZ.csv”); function(response, status, xhr){ var filename = “”; var disposition = xhr.getResponseHeader(‘Content-Disposition’); if (disposition && disposition.indexOf(‘attachment’) !== -1) { var filenameRegex = /filename[^;=\n]*=(([‘”]).*?\2|[^;\n]*)/; var … Read more

Avoiding content type issues when downloading a file via browser on Android

To make any downloads work on all (and especially older) Android versions as expected, you need to… set the ContentType to application/octet-stream put the Content-Disposition filename value in double quotes write the Content-Disposition filename extension in UPPERCASE Read my blog post for more details: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/

How to implement Content-Disposition: attachment?

Example of MP3 Lists: <a href=”https://stackoverflow.com/questions/8875949/download.php?file=testing.mp3″>Download MP3</a> <a href=”download.php?file=testing2.mp3″>Download MP3</a> download.php : <?php $file = $_GET[‘file’]; header(‘Content-type: audio/mpeg’); header(‘Content-Disposition: attachment; filename=”‘.$file.'”‘); ?>

Download textarea contents as a file using only Javascript (no server-side)

This may be what you are looking for: http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a-text-file-in-html5-using-javascrip/ It uses the browser’s download dialogue, but supports only FF and Chrome, and maybe more browsers now? function saveTextAsFile(textToWrite, fileNameToSaveAs) { var textFileAsBlob = new Blob([textToWrite], {type:’text/plain’}); var downloadLink = document.createElement(“a”); downloadLink.download = fileNameToSaveAs; downloadLink.innerHTML = “Download File”; if (window.webkitURL != null) { // Chrome allows … Read more

Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server

Just remove the output buffering (ob_start() and the others). Use just this: ftp_get($conn_id, “php://output”, $file, FTP_BINARY); Though if you want to add Content-Length header, you have to query file size first using ftp_size: $conn_id = ftp_connect(“ftp.example.com”); ftp_login($conn_id, “username”, “password”); ftp_pasv($conn_id, true); $file_path = “remote/path/file.zip”; $size = ftp_size($conn_id, $file_path); header(“Content-Type: application/octet-stream”); header(“Content-Disposition: attachment; filename=” . basename($file_path)); … Read more