限制Web客户端DownloadFile最大文件大小 [英] Limit WebClient DownloadFile maximum file size

查看:205
本文介绍了限制Web客户端DownloadFile最大文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP .NET项目,我的主页收到URL作为一个参数,我需要在内部下载,然后对其进行处理。我知道,我可以使用Web客户端的DownloadFile方法,但我想,以避免恶意用户从我的服务器到一个巨大的文件提供一个网址,这将不必要的流量。为了避免这种情况,我正在寻找一个解决方案来设置最大文件大小DownloadFile将下载。

In my asp .net project, my main page receives URL as a parameter I need to download internally and then process it. I know that I can use WebClient's DownloadFile method however I want to avoid malicious user from giving a url to a huge file, which will unnecessary traffic from my server. In order to avoid this, I'm looking for a solution to set maximum file size that DownloadFile will download.

感谢你在前进,

杰克

推荐答案

有没有办法做到这一点'干净',而无需使用Flash或Silverlight文件上载控件。您无需使用这些方法所能做的最好的办法是将的maxRequestLength 在你的web.config文件。

There is no way to do this 'cleanly' without using a flash or silverlight file upload control. The best you can do without using those methods would be to set the maxRequestLength in your web.config file.

例如:

<system.web>
    <httpRuntime maxRequestLength="1024"/>

上面的例子将限制文件大小为1MB。如果用户试图发送任何较大的,他们会得到一个错误消息,指出最大请求长度已经超过了。这不是一个pretty的消息,虽然,但如果你愿意,你可以重写在IIS中的错误页面,使之符合您的网站可能。

The example above will limit the file size to 1MB. If the user tries to send anything larger they will get an error message stating that the maximum request length has been exceeded. It's not a pretty message though but if you want you can override the error page in IIS to make it match your site possibly.

修改DUE TO评论:

所以,你可能使用两个方法做的就是将文件从URL,所以我会发布2可能的解决方案的要求。首先是使用.NET Web客户端

So your probably using a couple of methods to do the request to get the file from the URL so I will post 2 possible solutions. First is using the .NET WebClient:

// This will get the file
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadCompleted);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
webClient.DownloadFileAsync(new Uri("http://www.somewhere.com/test.txt"), @"c:\test.txt");

private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    WebClient webClient = (WebClient)(sender);
    // Cancel download if we are going to download more than we allow
    if (e.TotalBytesToReceive > iMaxNumberOfBytesToAllow)
    {
        webClient.CancelAsync();
    }
}

private void DownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    // Do something
}

另一种方法是,只是做一个基本的web请求做下载来检查文件的大小前:

The other method was to just do a basic web request before doing the download to check the file size:

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
}

希望这些解决方案中的一个帮助,或者至少让你在正确的道路上。

Hopefully one of these solutions help or at least get you on the right path.

这篇关于限制Web客户端DownloadFile最大文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆