作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 ServiceStack 用于一个简单的 Web 应用程序。主要目的是让用户下载文件。我正在使用 HttpResult 如下:
public class FileDownloadService : Service
{
public object Any()
{
string fileFullPath = "...";
string mimeType = "application/pdf";
FileInfo fi = new FileInfo(fileFullPath);
byte[] reportBytes = File.ReadAllBytes(fi.FullName);
result = new HttpResult(reportBytes, mimeType);
return result;
}
}
这会在用户浏览器中打开一个对话框,用户可以指定保存文件的位置。指定一个默认名称,即ServiceStack的restPath的名称。
我的问题是:是否可以为用户选择保存时指定自定义文件名(更改默认文件名)?我尝试使用 HttpResult 属性,但没有成功。提前致谢!
最佳答案
您应该在 HTTP 结果上设置“Content-Disposition” header 。这允许设置文件名:
public object Any()
{
string fileFullPath = "...";
string mimeType = "application/pdf";
FileInfo fi = new FileInfo(fileFullPath);
byte[] reportBytes = File.ReadAllBytes(fi.FullName);
result = new HttpResult(reportBytes, mimeType);
result.Headers.Add("Content-Disposition", "attachment;filename=YOUR_NAME_HERE.pdf;");
return result;
}
关于c# - 使用 ServiceStack HttpResult : how to specify a file name for downloaded content? 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38524897/
我是一名优秀的程序员,十分优秀!