.NET上的实现下载的如下代码怎么把它转成c#窗体应用程序能用的代码呢??或者有另外的方法么?
protectedvoidButton1_Click(objectsender,EventArgse){stringfilePath=@"E:\a.exe";//存到服务...
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = @"E:\a.exe";//存到服务器端的文件完整路径
Uri rl = new Uri("http://192.168.1.65/web/tep.exe");//远程文件url
try
{
using (WebClient wc = new WebClient())
{
wc.DownloadFile(rl, filePath);//存到服务器端
//以下代码实现下载到客户端.
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filePath, System.Text.Encoding.UTF8));
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
}
}
catch (Exception ex)
{
Response.Write(ex.StackTrace.ToString());
Response.Write(ex.Message);
}
} 展开
{
string filePath = @"E:\a.exe";//存到服务器端的文件完整路径
Uri rl = new Uri("http://192.168.1.65/web/tep.exe");//远程文件url
try
{
using (WebClient wc = new WebClient())
{
wc.DownloadFile(rl, filePath);//存到服务器端
//以下代码实现下载到客户端.
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filePath, System.Text.Encoding.UTF8));
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
}
}
catch (Exception ex)
{
Response.Write(ex.StackTrace.ToString());
Response.Write(ex.Message);
}
} 展开
3个回答
展开全部
WebClient client;
public void DownLoad()
{
string filename = "文件名";//带后缀
string url = 地址 + filename;
//string url = "http://192.168.1.65/web/tep.exe";
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = filename;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filename = sfd.FileName;
ThreadDownLoadFile();
}
}
//使用多线程下载
private void ThreadDownLoadFile()
{
Thread thread = new Thread(() =>
{
client = new WebClient();
client.DownloadFile(url, filename);
});
thread.IsBackground = true;
thread.Start();
}
追问
使用多线程后为什么原来的控件不显示了呢?
展开全部
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.RestoreDirectory = true;
try
{
saveDialog.Filter = "Excel文件(*.xls)|*.xls";//设置打开文件的格式
saveDialog.FileName = "工资条信息(" + DateTime.Now.ToLongDateString() + ")";
saveDialog.ShowDialog();
if (Generate(saveDialog.FileName))
{
MessageBox.Show("生成成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("生成失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
throw ex;
}
saveDialog.RestoreDirectory = true;
try
{
saveDialog.Filter = "Excel文件(*.xls)|*.xls";//设置打开文件的格式
saveDialog.FileName = "工资条信息(" + DateTime.Now.ToLongDateString() + ")";
saveDialog.ShowDialog();
if (Generate(saveDialog.FileName))
{
MessageBox.Show("生成成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("生成失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
throw ex;
}
追问
我要的是一个C#窗体应用程序具备远程文件下载功能...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以上代码只是发送文件
思路:
服务器端发送文件,客户端接收
远程传输:
请百度"C#通过Http服务上传下载"
局域网:
请百度"Sockets C# 文件"
思路:
服务器端发送文件,客户端接收
远程传输:
请百度"C#通过Http服务上传下载"
局域网:
请百度"Sockets C# 文件"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询