c# 根据下载请求动态重命名文件
尝试下载文件时是否可以重命名文件? 例如,我想使用文件的 ID 将文件存储到文件夹中,但是当用户下载文件时我想返回原始文件名。
Is it possible to rename file when trying to download it?
For example, I would like to store files to folders using their id's but when user downloads file I'd like to return the original filename.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在此处更改文件名,
例如
在 ASP 中使用“另存为”对话框下载文件.NET
just change name of file over here
for example
Downloading a File with a Save As Dialog in ASP.NET
名称 = 档案名称 + 扩展名 (ejemplo.txt)
nombre = nombre del archivo + extension (ejemplo.txt)
我正在使用 C# 中的 API 控制器,并且我的请求的返回需要是
IHttpActionResult
经过几个小时的研究,这是我的解决方案。
作为我的请求的回报,我使用 ApiController.cs 中的
Content
方法:我必须创建一个自定义 MediaTypeFormatter,它是:
控制器中的方法如下所示:
为了解释,这当 ApiController 必须返回 HttpRequest 时,能够覆盖 ApiController 的默认行为,正如您所看到的,您可以更改返回流中写入的内容,此外您还可以更改内容配置(在其中设置文件名)。
最后,在此自定义 MediaTypeFormatter 的构造函数中,我使用静态 utils 类中的方法检索文档的标题,该方法是:
I am working with an API controller in C# and the return of my request needed to be
IHttpActionResult
After several research hours, here is my solution.
As a return for my request, I'm using
Content
method from ApiController.cs :I had to create a custom MediaTypeFormatter which is :
And the method in the controller looks like this :
For the explanation, this ables to override the default behavior of ApiController when it has to return an HttpRequest, as you can see you are able to change what is written is the returned stream, besides you're able to change content disposition, where you set the file name.
Finally, in the constructor of this custom MediaTypeFormatter, I retrieve the Title of the document using a method in a static utils class, which is :