C# 下载时,下载功能不显示文件的总大小 protectedvoid下载函数(字符串文件名) { 字符串文件路径=@“D:\XtraFiles\”+文件名; 字符串contentType=“应用程序/x-newton-compatible-pkg”; 流iStream=null; //用于读取区块中1024K字节的缓冲区 字节[]缓冲区=新字节[1048576]; //文件的长度: 整数长度; //要读取的总字节数: 长数据存储; 尝试 { //打开文件。 iStream=新文件流(filepath,FileMode.Open,FileAccess.Read,FileShare.Read); //要读取的总字节数: dataToRead=iStream.Length; HttpContext.Current.Response.ContentType=ContentType; HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8)); //读取字节。 而(数据读取>0) { //验证客户端是否已连接。 if(HttpContext.Current.Response.IsClientConnected) { //读取缓冲区中的数据。 长度=iStream.Read(缓冲区,0,10000); //将数据写入当前输出流。 HttpContext.Current.Response.OutputStream.Write(缓冲区,0,长度); //将数据刷新到HTML输出。 HttpContext.Current.Response.Flush(); 缓冲区=新字节[10000]; dataToRead=dataToRead-长度; } 其他的 { //如果用户断开连接,防止无限循环 dataToRead=-1; } } } 捕获(例外情况除外) { //捕获错误(如果有)。 HttpContext.Current.Response.Write(“错误:+ex.Message+”); HttpContext.Current.Response.ContentType=“text/html”; HttpContext.Current.Response.Write(“错误:找不到文件”); } 最后 { 如果(iStream!=null) { //关闭文件。 iStream.Close(); } HttpContext.Current.Response.End(); HttpContext.Current.Response.Close(); } }

C# 下载时,下载功能不显示文件的总大小 protectedvoid下载函数(字符串文件名) { 字符串文件路径=@“D:\XtraFiles\”+文件名; 字符串contentType=“应用程序/x-newton-compatible-pkg”; 流iStream=null; //用于读取区块中1024K字节的缓冲区 字节[]缓冲区=新字节[1048576]; //文件的长度: 整数长度; //要读取的总字节数: 长数据存储; 尝试 { //打开文件。 iStream=新文件流(filepath,FileMode.Open,FileAccess.Read,FileShare.Read); //要读取的总字节数: dataToRead=iStream.Length; HttpContext.Current.Response.ContentType=ContentType; HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8)); //读取字节。 而(数据读取>0) { //验证客户端是否已连接。 if(HttpContext.Current.Response.IsClientConnected) { //读取缓冲区中的数据。 长度=iStream.Read(缓冲区,0,10000); //将数据写入当前输出流。 HttpContext.Current.Response.OutputStream.Write(缓冲区,0,长度); //将数据刷新到HTML输出。 HttpContext.Current.Response.Flush(); 缓冲区=新字节[10000]; dataToRead=dataToRead-长度; } 其他的 { //如果用户断开连接,防止无限循环 dataToRead=-1; } } } 捕获(例外情况除外) { //捕获错误(如果有)。 HttpContext.Current.Response.Write(“错误:+ex.Message+”); HttpContext.Current.Response.ContentType=“text/html”; HttpContext.Current.Response.Write(“错误:找不到文件”); } 最后 { 如果(iStream!=null) { //关闭文件。 iStream.Close(); } HttpContext.Current.Response.End(); HttpContext.Current.Response.Close(); } },c#,asp.net,C#,Asp.net,我的donwload功能工作正常,但当用户下载时,浏览器无法看到下载的总文件大小 所以现在浏览器显示下载8mb的?,而不是下载8mb的142mb 我错过了什么?您需要发送ContentLength-标题: protected void downloadFunction(string filename) { string filepath = @"D:\XtraFiles\" + filename; string contentType = "application/x-newto

我的donwload功能工作正常,但当用户下载时,浏览器无法看到下载的总文件大小

所以现在浏览器显示下载8mb的?,而不是下载8mb的142mb


我错过了什么?

您需要发送
ContentLength
-标题:

protected void downloadFunction(string filename)
{
    string filepath = @"D:\XtraFiles\" + filename;
    string contentType = "application/x-newton-compatible-pkg";

    Stream iStream = null;
    // Buffer to read 1024K bytes in chunk
    byte[] buffer = new Byte[1048576];

    // Length of the file:
    int length;
    // Total bytes to read:
    long dataToRead;

    try
    {
        // Open the file.
        iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

        // Total bytes to read:
        dataToRead = iStream.Length;
        HttpContext.Current.Response.ContentType = contentType;
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

        // Read the bytes.
        while (dataToRead > 0)
        {
            // Verify that the client is connected.
            if (HttpContext.Current.Response.IsClientConnected)
            {
                // Read the data in buffer.
                length = iStream.Read(buffer, 0, 10000);

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

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

                buffer = new Byte[10000];
                dataToRead = dataToRead - length;
            }
            else
            {
                //prevent infinite loop if user disconnects
                dataToRead = -1;
            }
        }
    }
    catch (Exception ex)
    {
        // Trap the error, if any.
        HttpContext.Current.Response.Write("Error : " + ex.Message + "<br />");
        HttpContext.Current.Response.ContentType = "text/html";
        HttpContext.Current.Response.Write("Error : file not found");
    }
    finally
    {
        if (iStream != null)
        {
            //Close the file.
            iStream.Close();
        }
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Close();
    }
}
这似乎是你所缺少的

如果您设置此选项,浏览器将知道期望值。否则,它将一直运行,直到您停止发送数据,并且它不知道距离结束还有多长时间

HttpContext.Current.Response.AddHeader(HttpRequestHeader.ContentLength, iStream.Length);

您可能还对CIH可以提供一种更简单的方式将文件发送给客户而无需自己担心流的方式感兴趣。

以Jeopardy的方式。。。“什么是内容长度标题?”顺便说一句,您应该能够使用TransmitFile或类似的;无需编写此代码,而且缓冲区的大小大大超过了HttpContext.Current.Response.AddHeader(“Content Length”,iStream.Length.ToString());成功了:)
Response.AddHeader("Content-Length", iStream.Length);