那位使用过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 请求期间发生异常。 \
我在本地测试还可以成功,只要一发布到网站上就会出现这个问题.
我的代码如下,最简单的,就是出现这个问题,
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 请求期间发生异常。 \
我在本地测试还可以成功,只要一发布到网站上就会出现这个问题.
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 请求期间发生异常。
我在本地测试还可以成功,只要一发布到网站上就会出现这个问题.
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);
} }
}
这个地址我确定我可以访问
通过其它客户机访问,那也是要服务器能访问那个地址才行.你的代码是在web server上执行的.我一直就怀疑你思路就有问题.你在服务器端做这种事干啥?
你在本机测试成功是因为你的本机即是客户机又是web server.
你的服务器上能访问http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个地址吗?
还有给小瑕疵:
const string filePath = @"E:\\a.jpg"; 要么这样写:const string filePath = @"E:\a.jpg";
或者这样写:const string filePath = "E:\\a.jpg";
你那样写.只会是你的web server去访问http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个地址,然后把这个jpg文件存到你服务器的e盘.
你是想实现这个?你要想实现这功能,那你的程序写的就对,
要不是实现这功能,程序写的就不对.还有.回16楼
@"E:\\a.jpg"; 他把路径写成这样.是有点小问题,但不影响程序执行,还是会当成@"E:\a.jpg"处理,我刚才试了一下.
你自己好好看看你这段代码吧,然后好好想想
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);
}
}
2、确定是否有权限访问以上文件夹
3、确定是否有在本地建立文件的权限
你使用webclient.downloadfile方法是这样"a.jpg"的路径是把文件存到服务器上的那这样的路径"E:\\a.jpg"是存到那里?
wc.DownloadFile(rl, filePath);这行代码出错是因为你把http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个jpg文件下载到服务器端e盘时候没有权限,所以会出异常.
你照我的说法做一下.或者你对你网站所在的目录添加aspnet和network services用户,然后把文件存到你网站目录下.权限设定好后.你通过一台客户机去访问你的aspx,如果不报异常就正明成功下载,这时你到服务器上看去吧.看看是不是我所说的,把http://192.168.11.107/uploadfile/韩晓华-儿科学第九讲.jpg这个东西存到了服务器上.
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);
}
我说的够多的了.希望你的问题能够解决
lovehongyun 真有耐心啊。
我可以帮你写一下.等..
{
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);
} }
别用b/s的程序在服务器上转一下,直接可以在客户端通过一个小程序访问你的远程地址.然后下载到本地,这个时候你可以随便定那个本地的地址.b/s模式下,你从服务器端下载数据.server端是把响应流发到客户端.然后客户端决定存在哪里.
(他是不能直接操作你客户机电脑的,如果能操作就没有什么安全可言了.除非用activex有一定的权限才可以)而你现在的程序(我改那个)是服务器端把远程资源下载到服务器上.然后发向客户端,实现客户下载.
做客户端,在客户端上访问服务器,可以用这个方法直接下载到客户端所在的本地目录是吗?也就是像我上面所指的目录就不会出错了是吧?
==========
可以,可以写个winForm的程序.写个winForm程序直接访问你那个远程地址就OK了.就用你上面的方法就是我要实现的这样的不安全的,所以现在.net不支持这个功能,所以只有通过其它的方法实现,如你上所说的做一个客户端就可以了.
============
不是.net不支持这个功能,而是所有b/s的技术都不能这样搞,b/s的程序操作客户端的机器是有安全限制的,只能把响应流发给客户端,然后让客户端决定存在哪里.