下载:http://pub.alimama.com/report/getTbkPaymentDetails.json?spm=0.0.0.0.Oq98lr&queryType=1&payStatus=3&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime=2014-12-05&endTime=2014-12-11
比如这个是阿里妈妈的数据报表。是需要执行url后才会获得下载文件。
直接运行ie是可以获得下载文件。并弹出保存对话框。
那么如何通过c#实现下载这样链接所生成的文件
百度的方法试了,但是下载下来的是页面信息,并不是生成的xls文件
代码如下:
/// <summary>
/// 下载需要得xls
/// </summary>
private string DownloadFile(string starttime, string endtime, int p)
{
string fileName = "";
try
{
object[] args = new object[] { starttime, endtime, p, Http.Token };
string url = string.Format("http://pub.alimama.com/report/getTbkPaymentDetails.json?queryType=1&payStatus=&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime={0}&endTime={1}&toPage={2}&perPageSize=100&_tb_token_={3}&_input_charset=utf-8", args);
fileName = CreateFileName(url);
if (!Directory.Exists(Application.StartupPath + "/"))
{
Directory.CreateDirectory(Application.StartupPath + "/");
}
WebClient client = new WebClient();
client.DownloadFile(url, Application.StartupPath + "/" + fileName);
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
if (!String.IsNullOrEmpty(fileName))
{
Console.WriteLine("文件下载成功,文件名称:" + fileName);
}
else
{
Console.WriteLine("文件下载失败");
}
Console.ReadLine();
return fileName;
}
/// <summary>
/// 创建文件名称
/// </summary>
public static string CreateFileName(string url)
{
string fileName = "";
string startTime = url.Substring(url.IndexOf("startTime=") + 10, 10);
string endTime = url.Substring(url.IndexOf("endTime=") + 8, 10);
fileName = "TaokeDetail" + startTime + "-" + endTime + ".xls";
Console.WriteLine(fileName);
return fileName;
}
5 个解决方案
#1
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
client.DownLoad(url,"本地路径");
#2
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
#3
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
#4
WebClient client = new WebClient();
client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx");
var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx");
req.Method = "GET";
req.ContentType = "application/vnd.ms-excel";//EXCEL
req.Timeout = 30000;
var rsp = (HttpWebResponse)req.GetResponse();
var requestStream = rsp.GetResponseStream();
if (requestStream != null)
{
using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write))
{
requestStream.CopyTo(fileStream);
}
}
client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx");
var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx");
req.Method = "GET";
req.ContentType = "application/vnd.ms-excel";//EXCEL
req.Timeout = 30000;
var rsp = (HttpWebResponse)req.GetResponse();
var requestStream = rsp.GetResponseStream();
if (requestStream != null)
{
using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write))
{
requestStream.CopyTo(fileStream);
}
}
#5
#1
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
client.DownLoad(url,"本地路径");
#2
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
#3
WebClient client=new WebClient();
client.DownLoad(url,"本地路径");
哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
#4
WebClient client = new WebClient();
client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx");
var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx");
req.Method = "GET";
req.ContentType = "application/vnd.ms-excel";//EXCEL
req.Timeout = 30000;
var rsp = (HttpWebResponse)req.GetResponse();
var requestStream = rsp.GetResponseStream();
if (requestStream != null)
{
using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write))
{
requestStream.CopyTo(fileStream);
}
}
client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx");
var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx");
req.Method = "GET";
req.ContentType = "application/vnd.ms-excel";//EXCEL
req.Timeout = 30000;
var rsp = (HttpWebResponse)req.GetResponse();
var requestStream = rsp.GetResponseStream();
if (requestStream != null)
{
using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write))
{
requestStream.CopyTo(fileStream);
}
}