0

这是电话...

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

[请注意,我将内容长度设置为 null 因为我不知道(文件在另一台服务器上)]

a.AssetPath 是:“ http://http.cdnlayer.com/account name/folder/folder/folder/asset.mp3”

(这个例子的 URL 是假的,但在我的实现中我可以直接浏览文件,但是这个附件方法不起作用)

这是实现...

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());
        }
    }

该文件正在像我想要的那样直接下载(不是由浏览器打开)但是它不是文件,它只是一个同名的 0kb 文件。

4

1 回答 1

1

您只能通过使用例如FileResult. 您不能以这种方式从 URL 强制下载文件作为附件。你告诉浏览器的只是后面是一个附件,你给它一个 URL 作为保存它的名称。

您需要自己读取文件,并将其作为一系列字节写入响应。

于 2010-09-13T18:28:40.740 回答