简体   繁体   English

如何获取文件的大小(托管在Web服务器上)?

[英]How To Get The Size Of A File (Hosted On A Web Server)?

I am using the WebClient class to download an .exe file from a web server. 我正在使用WebClient类从Web服务器下载.exe文件。 Here is the code I am using to download the file: 这是我用来下载文件的代码:

WebClient webClient = new WebClient();    
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadDataAsync(new Uri("http://www.blah.com/calc.exe")); 

My application has a ProgressBar which gets updated in the callback ( webClient_DownloadProgressChanged ): 我的应用程序有一个ProgressBar,它在回调中更新( webClient_DownloadProgressChanged ):

private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar.Value = (int)e.BytesReceived;
}

The problem I am having is I have to set the Maximum value for the progress bar dynamically. 我遇到的问题是我必须动态设置进度条的Maximum In other words I need to know the size of the file I am downloading before the download starts. 换句话说,我需要知道下载开始之前我正在下载的文件的大小。

Is there a way to get the size of a file (before downloading it) given it's uri? 有没有办法获得文件的大小(在下载之前),因为它是uri?

Try to set max size to e.TotalBytesToReceive like this 尝试将max size设置为e.TotalBytesToReceive,如下所示

private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
     progressBar.Max = (int)e.TotalBytesToReceive;
    progressBar.Value = (int)e.BytesReceived;

}

One of the ways will be checking for the Content-Length or the Range header in ResponseHeaders. 其中一种方法是检查ResponseHeaders中的Content-Length或Range标头。

// To set the range
//webClient.Headers.Add("Range","bytes=-128");

// To read Content-Length
var bytes = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);

If you only need to update progress bar correctly, the easiest way is to use ProgressPercentage 如果您只需要正确更新进度条,最简单的方法是使用ProgressPercentage

// progressBar.Max is always set to 100
progressBar.Value = e.ProgressPercentage;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Worker Role中托管的Web API中获取客户端服务器的唯一标识符 - How to get unique identifier of client server in Web API hosted in Worker Role 如何使用asp.net访问和修改托管在其他服务器中的项目的Web配置文件 - How to access and modify a web config file of a project hosted in different server using asp.net 如何在自托管的Web API上发送文件并在服务器上处理它? - How do I send a file on a self hosted web API and process it on the server? 如何在 web .net 核心中获取上传文件的文件大小 api - how to get file size of uploaded fiel in web api in .net core 从服务器托管应用程序时如何获取用户文件的本地桌面路径? - How to get the local desktop path of the users file when the application is hosted from server? 如何在同一服务器上托管的两个网站中共享同一会话? - How to share same session in two web sites hosted on same server? 在托管Web服务器中安排作业 - Schedule a job in hosted web server 如何获取访问我的网页的客户端的IP地址,其中我的网页或WebApp托管在某个远程服务器上 - How to get IP address of the Client accessing my page where My web page or WebApp is hosted at some remote server 如何使用 C# 获取托管在 IIS 中的所有 web 服务 - How to get all web services hosted in IIS using C# 如何从由ServiceHost托管的IIS Web服务访问文件 - How to access a file from IIS web service hosted by ServiceHost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM