C# 实现文件下载的问题。

z147912115 2011-10-11 11:48:38
想通过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;
}

}
}
...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_da 2012-07-27
  • 打赏
  • 举报
回复
同样遇到这个问题,想下载一个数据文件,但是得到的却是一段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());
}
}
}
z147912115 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhouyao85258 的回复:]

再把流写入一个新文件不就行了。。。
[/Quote]
我的代码里有写入文件。。
我的意思是得到的根本就不是要下载的那个文件。。

整个POST数据什么的。地址什么的我都是通过数据包截取查看。。应该无误。。
求高手解答。。。
lcaiyhh 2011-10-11
  • 打赏
  • 举报
回复

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");
zhouyao85258 2011-10-11
  • 打赏
  • 举报
回复
再把流写入一个新文件不就行了。。。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧