C# 中怎样将服务器端的文件下载到客户端

shinyML 2010-01-15 08:52:49
C# 中怎样将服务器端的文件下载到客户端
...全文
821 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyinfo 2010-01-19
  • 打赏
  • 举报
回复
up
shinyML 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wuyq11 的回复:]
ftpwebrequest下载文件
string filePath = @"";
        const string url = "";
        try
        {
            using (WebClient wc = new WebClient())
            {
                string html = wc.DownloadString(url);
                using (StreamWriter writer = new StreamWriter(filePath,false,wc.Encoding))
                {
                    writer.Write(html);
                    writer.Flush();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.Read();

[/Quote]

我用的是C# 语言
shinyML 2010-01-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xray2005 的回复:]
C# codepublicbool downfile(string url,string LocalPath)
  {try
  {
    Uri u=new Uri(url);
    HttpWebRequest mRequest= (HttpWebRequest)WebRequest.Create(u);
    mRequest.Method="GET";
    mRequest.ContentType="application/x-www-form-urlencoded";

    HttpWebResponse wr= (HttpWebResponse)mRequest.GetResponse();

    Stream sIn= wr.GetResponseStream();
    FileStream fs=new FileStream(LocalPath, FileMode.Create, FileAccess.Write);long length= wr.ContentLength;long i=0;decimal j=0;while (i < length)
    {byte[] buffer=newbyte[1024];
    i+= sIn.Read(buffer,0, buffer.Length);
    fs.Write(buffer,0, buffer.Length);if((i%1024)==0)
    {
      j=Math.Round(Convert.ToDecimal((Convert.ToDouble(i)/Convert.ToDouble(length))*100),4);
      statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节  当前下载大小:"+i+"字节 下载进度"+j.ToString()+"%";

    }else
    {
      statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节  当前下载大小:"+i+"字节";
    }

    }

    sIn.Close();
    wr.Close();
    fs.Close();returntrue;
  }catch {returnfalse; }
  }

OK
[/Quote]

是不是我设置好本地路径后,文件下载完成就在我设置的路径下面啊
shinyML 2010-01-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nimingxin1987 的回复:]
C# codepublicbool downfile(string url,string LocalPath)
{try
{
Uri u=new Uri(url);
HttpWebRequest mRequest= (HttpWebRequest)WebRequest.Create(u);
mRequest.Method="GET";
mRequest.ContentType="application/x-www-form-urlencoded";

HttpWebResponse wr= (HttpWebResponse)mRequest.GetResponse();

Stream sIn= wr.GetResponseStream();
FileStream fs=new FileStream(LocalPath, FileMode.Create, FileAccess.Write);long length= wr.ContentLength;long i=0;decimal j=0;while (i< length)
{byte[] buffer=newbyte[1024];
i+= sIn.Read(buffer,0, buffer.Length);
fs.Write(buffer,0, buffer.Length);if((i%1024)==0)
{
j=Math.Round(Convert.ToDecimal((Convert.ToDouble(i)/Convert.ToDouble(length))*100),4);
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节 下载进度"+j.ToString()+"%";

}else
{
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节";
}

}

sIn.Close();
wr.Close();
fs.Close();returntrue;
}catch {returnfalse; }
}
[/Quote]

url 是服务器端得路径吗

那Uri又是什么啊
wuyq11 2010-01-15
  • 打赏
  • 举报
回复
ftpwebrequest下载文件
string filePath = @"";
const string url = "";
try
{
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString(url);
using (StreamWriter writer = new StreamWriter(filePath,false,wc.Encoding))
{
writer.Write(html);
writer.Flush();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
xray2005 2010-01-15
  • 打赏
  • 举报
回复
C# codepublicbool downfile(string url,string LocalPath)
{try
{
Uri u=new Uri(url);
HttpWebRequest mRequest= (HttpWebRequest)WebRequest.Create(u);
mRequest.Method="GET";
mRequest.ContentType="application/x-www-form-urlencoded";

HttpWebResponse wr= (HttpWebResponse)mRequest.GetResponse();

Stream sIn= wr.GetResponseStream();
FileStream fs=new FileStream(LocalPath, FileMode.Create, FileAccess.Write);long length= wr.ContentLength;long i=0;decimal j=0;while (i < length)
{byte[] buffer=newbyte[1024];
i+= sIn.Read(buffer,0, buffer.Length);
fs.Write(buffer,0, buffer.Length);if((i%1024)==0)
{
j=Math.Round(Convert.ToDecimal((Convert.ToDouble(i)/Convert.ToDouble(length))*100),4);
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节 下载进度"+j.ToString()+"%";

}else
{
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节";
}

}

sIn.Close();
wr.Close();
fs.Close();returntrue;
}catch {returnfalse; }
}

OK
别递烟哥不会 2010-01-15
  • 打赏
  • 举报
回复
web中用网页下载 “打开 保存 取消”网上代码很多
不知道在winform中能不能用
nimingxin1987 2010-01-15
  • 打赏
  • 举报
回复

public bool downfile(string url,string LocalPath)
{
try
{
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();

Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);

long length = wr.ContentLength;
long i = 0;
decimal j=0;

while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);

if((i % 1024)==0)
{
j=Math.Round(Convert.ToDecimal((Convert.ToDouble(i)/Convert.ToDouble(length))*100),4);
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节 下载进度"+j.ToString()+"%";

}
else
{
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节";
}

}

sIn.Close();
wr.Close();
fs.Close();
return true;
}
catch { return false; }
}


逸客 2010-01-15
  • 打赏
  • 举报
回复
说清楚问题。
liguicd 2010-01-15
  • 打赏
  • 举报
回复
不懂C#只能给梅子顶
wazdoyang 2010-01-15
  • 打赏
  • 举报
回复
簡單的就 <A>
Teng_s2000 2010-01-15
  • 打赏
  • 举报
回复
什么意思啊,VS保护的文件你是没有办法下的 啊

别的诸如一些视频,图片,压缩包都可以通过流进行下载的啊
jerry_zhang99 2010-01-15
  • 打赏
  • 举报
回复
学习了
nimingxin1987 2010-01-15
  • 打赏
  • 举报
回复
是的
shinyML 2010-01-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nimingxin1987 的回复:]
C# codepublicbool downfile(string url,string LocalPath)
{try
{
Uri u=new Uri(url);
HttpWebRequest mRequest= (HttpWebRequest)WebRequest.Create(u);
mRequest.Method="GET";
mRequest.ContentType="application/x-www-form-urlencoded";

HttpWebResponse wr= (HttpWebResponse)mRequest.GetResponse();

Stream sIn= wr.GetResponseStream();
FileStream fs=new FileStream(LocalPath, FileMode.Create, FileAccess.Write);long length= wr.ContentLength;long i=0;decimal j=0;while (i< length)
{byte[] buffer=newbyte[1024];
i+= sIn.Read(buffer,0, buffer.Length);
fs.Write(buffer,0, buffer.Length);if((i%1024)==0)
{
j=Math.Round(Convert.ToDecimal((Convert.ToDouble(i)/Convert.ToDouble(length))*100),4);
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节 下载进度"+j.ToString()+"%";

}else
{
statusBar1.Text="当前下载文件大小:"+length.ToString()+"字节 当前下载大小:"+i+"字节";
}

}

sIn.Close();
wr.Close();
fs.Close();returntrue;
}catch {returnfalse; }
}
[/Quote]

localpath 是不是自己定义的本地路径啊

110,578

社区成员

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

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

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