c#指定したフォルダにネット上のファイルをダウンロード

1794 ワード

, 。

        string fileName = "aaa.txt";//         
        string filePath = Server.MapPath("DownLoad/aaa.txt");//  
        //           
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //              
        Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
 
  

,( )

public void DownloadImage(string url, string path)
        {
            WebRequest request = WebRequest.Create(url);
            request.ContentType = "impage/jpg";
            WebResponse response = request.GetResponse();
            using (Stream reader = response.GetResponseStream())
            {
                using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = new byte[512];
                    int c = 0;
                    while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        writer.Write(buffer, 0, c);
                    }
                }
            }