直接下载文件而不是打开处理程序-文件大小显示问题

本文关键字:程序 处理 文件大小 问题 显示 下载 文件 | 更新日期: 2023-09-27 18:12:31

请参见下面的处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FileExplorer
{
    /// <summary>
    /// Summary description for HandlerForMyFE
    /// </summary>
    public class HandlerForMyFE : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private HttpContext _context;
        private HttpContext Context
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            Context = context;
            string filePath = context.Request.QueryString["Downloadpath"];
            filePath = context.Server.MapPath(filePath);
            if (filePath == null)
            {
                return;
            }
            System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
            System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
            byte[] bytes = new byte[streamReader.BaseStream.Length];
            br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
            if (bytes == null)
            {
                return;
            }
            streamReader.Close();
            br.Close();
            string fileName = System.IO.Path.GetFileName(filePath);
            string MimeType = GetMimeType(fileName);
            string extension = System.IO.Path.GetExtension(filePath);
            char[] extension_ar = extension.ToCharArray();
            string extension_Without_dot = string.Empty;
            for (int i = 1; i < extension_ar.Length; i++)
            {
                extension_Without_dot += extension_ar[i];
            }
            //if (extension == ".jpg")
            //{ // Handle *.jpg and
            //    WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);
            //}
            //else if (extension == ".gif")
            //{// Handle *.gif
            //    WriteFile(bytes, fileName, "image/gif gif", context.Response);
            //}
            WriteFile(bytes, fileName, MimeType + " " + extension_Without_dot, context.Response);
        }
        private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)
        {
            response.Buffer = true;
            response.Clear();
            response.ContentType = contentType;
            response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            response.BinaryWrite(content);
            response.Flush();
            response.End();
        }
        private string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

我写的处理程序直接下载文件后,点击而不打开该文件在浏览器中…
看来上面的代码起作用了!
但是在下载时显示文件大小有问题。
要测试PLZ,请参阅下面的链接:
测试与互联网下载管理器
在那个链接中我们没有文件大小,但在下面的链接中我们有:
另一个链接
我的处理程序有什么问题,我该如何解决这个问题?

thanks in advance

直接下载文件而不是打开处理程序-文件大小显示问题

我没有运行这段代码。这是另一种传输文件的方法。

试试这个代码:

//context = HttpContext
context.Response.Clear();
context.Response.ContentType = varMimeType;
context.Response.TransmitFile(filePath);
context.Response.AddHeader("content-disposition", "attachment;filename=" + varFileName);
context.Response.AddHeader("Content-Length", filePathFileInfo.Length);
context.Response.Flush();
context.Response.End();