netcore from api download the file to a local

public IActionResult HttpDownFile()
        {
            string url = "http://localhost:99/api/HttpFile/GetFile";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            byte[] fileBytes;
            using (WebResponse webRes = request.GetResponse())
            {
                int length = (int)webRes.ContentLength;
                HttpWebResponse response = webRes as HttpWebResponse;
                Stream Stream = response.GetResponseStream ();
                 var contentDisposition Response.Headers = [ " the Content-Disposition " ];
                 var filename = Util.Midstr (contentDisposition, " filename = " , " ; " );
                 // read into memory 
                MemoryStream stmMemory = new new the MemoryStream ();
                 byte [] Buffer = new new  byte [length];
                 int I;
                 // byte into the byte-by in
                 the while ((I = Stream.Read (Buffer,0, buffer.Length)) > 0)
                {
                    stmMemory.Write(buffer, 0, i);
                }
                fileBytes = stmMemory.ToArray();//文件流Byte
                FileStream fs = new FileStream("D:\\other\\Test\\"+filename, FileMode.OpenOrCreate);
                stmMemory.WriteTo(fs);
                stmMemory.Close();
                fs.Close();
                return Ok("D:\\other\\Test\\" + filename);
            }
        }

url is the address to download the file

Guess you like

Origin www.cnblogs.com/huanyun/p/11350832.html