Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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
下载文件时不显示总文件大小和时间(C#)_C#_Asp.net Mvc - Fatal编程技术网

下载文件时不显示总文件大小和时间(C#)

下载文件时不显示总文件大小和时间(C#),c#,asp.net-mvc,C#,Asp.net Mvc,请有人告诉我,当从服务器向浏览器发送字节时,如何显示总文件大小和下载时间。 我正在尝试将大字节(1 Gb)逐字节发送到客户端。以下是我尝试的代码: long dataToRead; int length; byte[] buffer = new Byte[100000]; FileStream iStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); dataToRead = iStream.Length; Resp

请有人告诉我,当从服务器向浏览器发送字节时,如何显示总文件大小和下载时间。 我正在尝试将大字节(1 Gb)逐字节发送到客户端。以下是我尝试的代码:

long dataToRead;
int length;
byte[] buffer = new Byte[100000];
FileStream iStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
while (dataToRead > 0)
{
    // Verify that the client is connected.
    if (Response.IsClientConnected)
    {
        // Read the data in buffer.
        length = iStream.Read(buffer, 0, 10000);

        // Write the data to the current output stream.
        await Response.OutputStream.WriteAsync(buffer, 0, length);

        // Flush the data to the HTML output.
       Response.Flush();

        buffer = new Byte[100000];
        dataToRead = dataToRead - length;
    }
    else
    {
        dataToRead = -1;
    }
}

您可以尝试添加
内容长度
标题,让浏览器知道大小:

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", dataToRead.ToString());
// Remainder of your code

请注意,在您的代码示例中,即使在服务器端,您也不知道文件有多大。您希望如何告知客户并根据总规模报告进度?这是多么浪费人工编码。你为什么不直接使用返回文件流(iStream)?谢谢@Kyle Burns的回复,我只是想要那个代码,你能帮我写这样的代码吗?返回文件流(iStream)不会返回大小为1 GB的文件@Camilo Terevinto。我想知道你是从哪里收集到这些信息的。。。但是,您只需要添加一个具有文件大小的头。