Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 限制WebClient下载文件的最大文件大小_.net_Asp.net_Http_Webclient - Fatal编程技术网

.net 限制WebClient下载文件的最大文件大小

.net 限制WebClient下载文件的最大文件大小,.net,asp.net,http,webclient,.net,Asp.net,Http,Webclient,在我的asp.net项目中,我的主页接收URL作为参数,我需要在内部下载并处理它。我知道我可以使用WebClient的DownloadFile方法,但是我想避免恶意用户向一个巨大的文件提供url,这将导致我的服务器产生不必要的流量。为了避免这种情况,我正在寻找一种解决方案来设置DownloadFile将下载的最大文件大小 提前谢谢大家, Jack如果不使用flash或silverlight文件上载控件,就无法“干净地”完成此操作。如果不使用这些方法,最好在web.config文件中设置maxRe

在我的asp.net项目中,我的主页接收URL作为参数,我需要在内部下载并处理它。我知道我可以使用WebClient的DownloadFile方法,但是我想避免恶意用户向一个巨大的文件提供url,这将导致我的服务器产生不必要的流量。为了避免这种情况,我正在寻找一种解决方案来设置DownloadFile将下载的最大文件大小

提前谢谢大家,


Jack

如果不使用flash或silverlight文件上载控件,就无法“干净地”完成此操作。如果不使用这些方法,最好在web.config文件中设置
maxRequestLength

例如:

<system.web>
    <httpRuntime maxRequestLength="1024"/>
另一种方法是在下载之前只执行一个基本的web请求来检查文件大小:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://www.somewhere.com/test.txt"));
webRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Int64 fileSize = webResponse.ContentLength;
if (fileSize < iMaxNumberOfBytesToAllow)
{
    // Download the file
}
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(新Uri(“http://www.somewhere.com/test.txt"));
webRequest.Credentials=CredentialCache.DefaultCredentials;
HttpWebResponse webResponse=(HttpWebResponse)webRequest.GetResponse();
Int64 fileSize=webResponse.ContentLength;
if(文件大小
希望这些解决方案中的一个能帮助或至少让你走上正确的道路

var webClient = new WebClient();
client.OpenRead(url);
Int64 bytesTotal = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);

然后你决定
bytesTotal
是否在最大下载量或最大上传量的限制范围内?@Aristos-我说的是最大下载量。我的asp.net页面下载传递给它的url。@Kelsey-你的答案与此无关。请重读这个问题。@Jack Juiceson-您使用什么方法获取URL?您是否正在使用库来处理文件流?感谢您的重新编辑,DownloadProgressChanged的第一个解决方案是我将使用的,这就是我正在寻找的。关于建议首先执行请求的第二个解决方案,我不会使用它,因为服务器并不总是提供内容长度头。您自己测试过吗?带有
DownloadProgressChangedEventHandler
的解决方案不起作用
var webClient = new WebClient();
client.OpenRead(url);
Int64 bytesTotal = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);