C#項(xiàng)目API返回文件案例
string path = "你的文件地址/測(cè)試文件.txt"; var filestream = new FileStream(path, FileMode.OpenOrCreate); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(filestream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "測(cè)試文件.txt" }; return response;
MVC項(xiàng)目返回文件案例
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; byte[] fileContents="文件轉(zhuǎn)化后的byte"; return File(outfile, $"{fileContents};Charset=utf-8", $"行政費(fèi)用申請(qǐng){DateTime.Now.ToString("yyyy-MM-dd")}.xlsx");
本文摘自 :https://www.cnblogs.com/