[HttpGet] public IActionResult HttpReturnFile() { string url = "http://localhost:99/api/HttpFile/GetFile"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; using (WebResponse webRes = request.GetResponse()) { int length = (int)webRes.ContentLength; HttpWebResponse response = webRes as HttpWebResponse; Stream stream = response.GetResponseStream(); var contentdisposition = response.Headers["Content-Disposition"]; var filename = Util.Midstr(contentdisposition, "filename=", ";"); var ext = "."+filename.Split('.')[1]; new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype); //读取到内存 MemoryStream stmMemory = new MemoryStream(); byte[] buffer = new byte[length]; int i; //将字节逐个放入到Byte中 while ((i = stream.Read(buffer, 0, buffer.Length)) > 0) { stmMemory.Write(buffer, 0, i); } stmMemory.Close(); return File(buffer, contenttype ?? "application/octet-stream",filename); } }
url下载文件地址