这是电话...
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 文件。