引用
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;
using System.Threading.Tasks;
代码
[HttpPost]
public async Task<HttpResponseMessage> GetFileApi()
{
try
{
var FilePath = Path.Combine(Directory.GetCurrentDirectory(), "1.txt");
var stream = new FileStream(FilePath, FileMode.Open);
HttpResponseMessage response = new(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "1.txt"
};
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}