views:

46

answers:

1

here is the call...

return new FileUriResult("application/octet-stream", a.AssetPath, null);

[note that I set content length to null because I don't know it (file is on another server)]

a.AssetPath is: "http://http.cdnlayer.com/account name/folder/folder/folder/asset.mp3"

(fake URL for this example but in my implementation I can browse the file directly, however this attachment method is not working)

here is the implementation...

public class FileUriResult : ActionResult
    {
        private string _contentType;
        private string _fileUri;
        private long? _fileLength;

        public FileUriResult(string contentType, string fileUri, long? fileLength)
        {
            _contentType = contentType;
            _fileUri = fileUri;
            _fileLength = fileLength;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;

            response.ContentType = _contentType;
            response.AddHeader("Content-Disposition", "attachment; filename=" + _fileUri);
            if(_fileLength != null)
                response.AddHeader("Content-Length", _fileLength.ToString());
        }
    }

The file is being directly downloaded just like I want (not opened by browser) however it is NOT the file, it is simply a 0kb file with the same name.

+1  A: 

You can only force a download as an attachment by sending the binary data out using, for example, a FileResult. You can't force the download of a file as an attachment from a URL in this way. All you're telling the browser is that what follows is an attachment, and you're giving it the URL as the name with which to save it.

You'd need to read in the file yourself, and write it out to the response as a series of bytes.

David M
thanks, this makes sense, I always expect too much of technology
shogun