如何使用特定扩展名提供文件

问题描述 投票:0回答:1

我正在尝试使用js.net core应用程序下载文件,并且我希望服务器设置文件扩展名。

不使用File方法不知道如何设置它:

什么有效

public  FileResult DownloadFileAsync() {
            FileStream stream = new FileStream(FILEPATH, FileMode.Open, FileAccess.Read); 
                this.Response.ContentType = "application/octet-stream";
                this.Response.ContentLength = stream.Length;
                return File(stream, "application/force-download", "data2.txt");
                                                                          ^                                                     
        }

[我正在努力使它正常工作

public async Task DownloadAsync(long id) {
            using (FileStream stream = new FileStream(FILEPATH, FileMode.Open, FileAccess.Read)) {
                this.Response.ContentType = "application/octet-stream";
                this.Response.ContentLength = stream.Length;

                await stream.CopyToAsync(this.Response.Body);

            }
        }

P.S与第一种情况一样,如何不使用File方法来设置文件扩展名。response中还有另一个字段可以设置文件的type吗?

file asp.net-core stream
1个回答
0
投票

使用Reponse的content-disposion属性为我工作,在这种情况下,下载的文件将被命名为“ filename.jpg”:

'Content-Disposition':'附件; filename =“ filename.jpg”'

© www.soinside.com 2019 - 2024. All rights reserved.