1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public HttpResponseMessage ImportTemplate(string fileName) { var context = HttpContext.Current; var response = new HttpResponseMessage(HttpStatusCode.OK); var path = context.Server.MapPath("~/App_Data/" + fileName); if (string.IsNullOrEmpty(fileName) || !File.Exists(path)) { response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent($"<meta charset=\"utf-8\"><h3>【{fileName}】不存在。</h3>", Encoding.UTF8) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return response; } var stream = new FileStream(path, FileMode.Open, FileAccess.Read); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlEncode(fileName) }; response.Headers.Add("Access-Control-Expose-Headers", "FileName"); response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName)); return response; } |