to'g'ridan-to'g'ri faylni ishlov berish bilan ochish o'rniga, muammo ko'rsatadigan fayl hajmi

plz quyidagi ishlovchilarga qarang:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FileExplorer
{
    /// 
/// Summary description for HandlerForMyFE ///
 
    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;
            }
        }
    }
}

brauzerda ushbu faylni ochmasdan bosishdan keyin to'g'ridan-to'g'ri yuklab olish fayli uchun ishlovchilarni yozganman ... yuqori kodlar ishlaydi! lekin yuklab olish vaqtida fayl hajmi ko'rsatishda muammo yuzaga keladi plz quyidagi havolani ko'rsating:
Internetdan yuklab olish menejeri bilan test Ushbu linkda bizda fayl hajmi yo'q, ammo quyidagi havolada bizda mavjud:
boshqa havola
mening ishlovchilarimning muammosi va bu masalani qanday hal qilishim mumkin?

oldindan rahmat

1

1 javoblar

Ushbu kodni ishlatmadim. Faylni uzatishning yana bir usuli bu erda.

Ushbu kodni ko'ring:

//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();
2
qo'shib qo'ydi
Javobingiz uchun rahmat/i shu tarzda sinab ko'ring -> muammo hali ham mavjud
qo'shib qo'ydi muallif MoonLight, manba
Bro -> SUPER SOLVEDni rahmat
qo'shib qo'ydi muallif MoonLight, manba
Content-Length ni sinab ko'rdingizmi? Yangilangan javobni ko'ring.
qo'shib qo'ydi muallif Valamas, manba
Zo'r! Umid qilamanki, boshqa kod ham ishlatilgan.
qo'shib qo'ydi muallif Valamas, manba