想通过C#方法下载文件。。
但得到的数据流根本就是那个文件的流。。
求高手解答。。
代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.IO;namespace yunfile
{
    public partial class _Default : System.Web.UI.Page
    {
        //System.Net.ServicePointManager.Expect100Continue = false;
        protected void Page_Load(object sender, EventArgs e)
        {        }        protected void btn_DownLoad_Click(object sender, EventArgs e)
        {
            string YunFileAddress = "http://dl24.yunfile.com/file/downfile/z147912115/ee2de7de/fa676205";
            string postString = "module=fileService&action=downfile&userId=z147912115&fileId=ee2de7de&vid=fa676205&vid1=9b28f2fd2b31b950";
            CookieContainer cookieContainer = new CookieContainer();
            string result = SendDataByPost(YunFileAddress.ToString(), postString,ref cookieContainer);            //FileDownload(result);
            tb_ResponseWeb.Text = result;        }
        //保存文件
        public void FileDownload(string str)
        {
            FileStream fs = new FileStream("D:/1/1.txt", FileMode.Create);
            //获得字节数组
            byte[] data = new UTF8Encoding().GetBytes(str);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
        }
        //POST数据并获取返回页面
        public string SendDataByPost(string Url, string postDataStr, ref CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            if (cookie.Count == 0)
            {
                request.CookieContainer = new CookieContainer();
                cookie = request.CookieContainer;
            }
            else
            {
                request.CookieContainer = cookie;
            }            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();            return retString;
        }    }
}

解决方案 »

  1.   


                System.Net.WebClient wc = new System.Net.WebClient();
                wc.DownloadFile("http://avatar.profile.csdn.net/3/2/2/2_z147912115.jpg", "d:\\2_z147912115.jpg");
      

  2.   

    我的代码里有写入文件。。
    我的意思是得到的根本就不是要下载的那个文件。。整个POST数据什么的。地址什么的我都是通过数据包截取查看。。应该无误。。
    求高手解答
      

  3.   

    同样遇到这个问题,想下载一个数据文件,但是得到的却是一段HTML代码这是我的代码,帖子地址:http://topic.csdn.net/u/20120727/16/a1794b78-63cb-490a-895f-47b866f85b9b.html
    实在是没分了,不好意思,只有20分。感谢各位的热情回复!这是我的方法:问题:下载后的文件不是想要的CSV数据文件,而是一段html代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;   
    using System.Configuration;
    using System.Web;   
    namespace mydownload
    {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }  string filename=@"F:\file.csv";
      private void button1_Click(object sender, EventArgs e)
      {   
      string desAdrss = textBox1.Text;
      System.Net.WebClient myWebClient = new System.Net.WebClient();
      myWebClient.Credentials = new System.Net.NetworkCredential(textBox2.Text, textBox3.Text);   
      try
      {
      myWebClient.DownloadFile(desAdrss, filename);
      }
      catch (Exception ee)
      {
      MessageBox.Show(ee.ToString());
      }
      }
     }