那位使用过WebClient.DownLoadFile(string,string)下载文件,请帮我解决一下一个问题
我的代码如下,最简单的,就是出现这个问题,
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        const string filePath = @"E:\\a.sitemap";
        Uri rl = new Uri("http://192.168.11.23/webclentapp/Web.sitemap");
        try
        {
            using (WebClient wc = new WebClient())
            {
                wc.DownloadFile(rl, filePath);                //string html = wc.DownloadString(url);
                //using (StreamWriter writer = new StreamWriter(filePath, false, wc.Encoding))
                //{
                //    writer.Write(html);
                //    writer.Flush();
                //}
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }    }
}
\要下载的文件是我网站目录里的文件,怎么老是出现如下问题.在 WebClient 请求期间发生异常。 \
我在本地测试还可以成功,只要一发布到网站上就会出现这个问题.

解决方案 »

  1.   

            const string filePath = @"E:\\a.jpg";
            Uri rl = new Uri("http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg");
            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadFile(rl, filePath);                //string html = wc.DownloadString(url);
                    //using (StreamWriter writer = new StreamWriter(filePath, false, wc.Encoding))
                    //{
                    //    writer.Write(html);
                    //    writer.Flush();
                    //}
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }正确的代码是这样的
    在 WebClient 请求期间发生异常。 
    我在本地测试还可以成功,只要一发布到网站上就会出现这个问题. 
      

  2.   

    远程的地址有改吗?Uri rl = new Uri("http://192.168.11.23/webclentapp/Web.sitemap"); 把192.168.11.23改掉~~~~~~~~~~~~
      

  3.   

    我试了一下,没问题,没出异常using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;
    using System.Net;public partial class Default19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string filePath = @"d:\a.txt";
            Uri rl = new Uri("http://localhost/GemsaTest/a.txt");
            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadFile(rl, filePath);                //string html = wc.DownloadString(url); 
                    //using (StreamWriter writer = new StreamWriter(filePath, false, wc.Encoding)) 
                    //{ 
                    //    writer.Write(html); 
                    //    writer.Flush(); 
                    //} 
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }     }    
    }
      

  4.   

    你确定你发布后的那台机器能访问http://192.168.11.23/webclentapp/Web.sitemap这个地址吗?
      

  5.   

    没发布.但如果发布后.只要你的server能访问那个地址,应该是没问题的.
      

  6.   

    http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg
    这个地址我确定我可以访问
      

  7.   


    通过其它客户机访问,那也是要服务器能访问那个地址才行.你的代码是在web server上执行的.我一直就怀疑你思路就有问题.你在服务器端做这种事干啥?
    你在本机测试成功是因为你的本机即是客户机又是web server.
      

  8.   


    你的服务器上能访问http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个地址吗?
      

  9.   

    你的程序那样写,只能是服务器请求http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg 这个地址,然后把这个文件保存在你的服务器的E:\a.jpg
      

  10.   

    改成你的发布站点的ip地址,192.168.11.23你是局域网的ip,所以你本地可以下载,但是在远程就不能下载了。
    还有给小瑕疵:
    const string filePath = @"E:\\a.jpg"; 要么这样写:const string filePath = @"E:\a.jpg"; 
    或者这样写:const string filePath = "E:\\a.jpg"; 
      

  11.   

    我是要把我的服务器上的这个文件存到本地名为 a.jpg
      

  12.   


    你那样写.只会是你的web server去访问http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个地址,然后把这个jpg文件存到你服务器的e盘.
    你是想实现这个?你要想实现这功能,那你的程序写的就对,
    要不是实现这功能,程序写的就不对.还有.回16楼
    @"E:\\a.jpg"; 他把路径写成这样.是有点小问题,但不影响程序执行,还是会当成@"E:\a.jpg"处理,我刚才试了一下.
      

  13.   

    你急什么?
    你自己好好看看你这段代码吧,然后好好想想
    protected void Page_Load(object sender, EventArgs e)
        {
            const string filePath = @"E:\\a.jpg"; 
            Uri rl = new Uri("http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg"); 
            try 
            { 
                using (WebClient wc = new WebClient()) 
                { 
                    //这行代码它会去请求你rl的那个地址,然后把那个jpg存到你的本机e盘改名为a.jpg,你发布到server上,所以server就成了那个本机
                      //我知道你的想法不是这样的.可你的代码实现的就是我所说的.
                    //你是想把这个文件存到客户端去,也就是存到访问这个页面的客户机上去,所以这你样写不对.
                    wc.DownloadFile(rl, filePath); 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
        } 
      

  14.   

    1、确定路径是否正确,包括http://192.168.11.23/和保存的时候的本地文件
    2、确定是否有权限访问以上文件夹
    3、确定是否有在本地建立文件的权限
      

  15.   


    你使用webclient.downloadfile方法是这样"a.jpg"的路径是把文件存到服务器上的那这样的路径"E:\\a.jpg"是存到那里?
      

  16.   

    lz,你的问题就是出现异常,是在wc.DownloadFile(rl, filePath); 这行代码处吧?如果是在这行出错我告诉你怎么解决,解决后你在看一下是不是你想要的结果.然后你就知道我上边说的是什么意思了.首先你在你的服务器E:盘建一个test的文件,然后对这个文件夹加上aspnet用户network services(window2003)用户
    wc.DownloadFile(rl, filePath);这行代码出错是因为你把http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个jpg文件下载到服务器端e盘时候没有权限,所以会出异常.
    你照我的说法做一下.或者你对你网站所在的目录添加aspnet和network services用户,然后把文件存到你网站目录下.权限设定好后.你通过一台客户机去访问你的aspx,如果不报异常就正明成功下载,这时你到服务器上看去吧.看看是不是我所说的,把http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个东西存到了服务器上.
      

  17.   

    添加aspnet,network services用户时记得给他们添加可写的权限.
      

  18.   


    const string filePath = @"D:\test\DetailsView\a.txt";//例如地址是这个,那你就把D:\test\DetailsView这个目录添加上aspnet,net workservices用户并赋写权限
            Uri rl = new Uri("http://192.168.0.136/GemsaTest/a.txt");
            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadFile(rl, filePath);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.StackTrace.ToString());
                Response.Write(ex.Message);
            } 
    我说的够多的了.希望你的问题能够解决
      

  19.   


    lovehongyun 真有耐心啊。
      

  20.   

    其实你的程序现在是把一个远程的jpg文件存到了服务器上,你只要再添加一步,把这个文件从服务器上下载到客户端就可以了.你把那个远程的jpg文件存到你的网站目录的一个文件夹中.然后把那个文件输出到客户端就OK了.
    我可以帮你写一下.等..
      

  21.   

    我帮你改了,你照着写即可protected void Page_Load(object sender, EventArgs e)
        {
            string filePath = Server.MapPath(".") + @"\a.txt";//存到服务器端的文件完整路径
            Uri rl = new Uri("http://192.168.0.136/GemsaTest/a.txt");//远程文件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.Filter.Close();
                        Response.WriteFile(file.FullName);
                        Response.End();
                    }            }
            }
            catch (Exception ex)
            {
                Response.Write(ex.StackTrace.ToString());
                Response.Write(ex.Message);
            }     }   
      

  22.   

    你直接写个客户端的程序不就行了.
    别用b/s的程序在服务器上转一下,直接可以在客户端通过一个小程序访问你的远程地址.然后下载到本地,这个时候你可以随便定那个本地的地址.b/s模式下,你从服务器端下载数据.server端是把响应流发到客户端.然后客户端决定存在哪里.
    (他是不能直接操作你客户机电脑的,如果能操作就没有什么安全可言了.除非用activex有一定的权限才可以)而你现在的程序(我改那个)是服务器端把远程资源下载到服务器上.然后发向客户端,实现客户下载.
      

  23.   

    就是我要实现的这样的不安全的,所以现在.net不支持这个功能,所以只有通过其它的方法实现,如你上所说的做一个客户端就可以了.
    做客户端,在客户端上访问服务器,可以用这个方法直接下载到客户端所在的本地目录是吗?也就是像我上面所指的目录就不会出错了是吧?
      

  24.   

    做客户端,在客户端上访问服务器,可以用这个方法直接下载到客户端所在的本地目录是吗?也就是像我上面所指的目录就不会出错了是吧?
    ==========
    可以,可以写个winForm的程序.写个winForm程序直接访问你那个远程地址就OK了.就用你上面的方法就是我要实现的这样的不安全的,所以现在.net不支持这个功能,所以只有通过其它的方法实现,如你上所说的做一个客户端就可以了. 
    ============
    不是.net不支持这个功能,而是所有b/s的技术都不能这样搞,b/s的程序操作客户端的机器是有安全限制的,只能把响应流发给客户端,然后让客户端决定存在哪里.