سؤال

أقوم ديناميكيًا بإنشاء ملف مضغوط في صفحة ASP.NET ثم إرسال الدفق إلى Response.

في Firefox، يمكنني تنزيل الملف المسمى Images.zip.إنه يعمل بشكل صحيح.في Internet Explorer 7 يحاول تنزيل ملف يسمى ZipExport.aspx أو إذا كان في معالج عام، ZipExport.ashx ويقول أنه لا يمكن العثور عليه على الخادم ويفشل.

هذا هو الكود الخاص بي:

Response.BufferOutput = true;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(System.TimeSpan.Zero);
ZipFile zip = new ZipFile();
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);

لا أرغب في إنشاء HTTPHandler لملف معين وتسجيله في IIS.

هل هناك شيء بسيط أفتقده أم أن Internet Explorer مخطئ لأنه تجاهل رأس ترتيب المحتوى الخاص بي؟

يحرر:لقد قمت بإزالة هذه السطور وعملت الأشياء:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

يحرر:إليك رمز العمل إذا كان أي شخص مهتمًا:

public void ProcessRequest(HttpContext context)
{  
    context.Response.Clear();
    context.Response.BufferOutput = false;
    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader("content-disposition", 
        "attachment; filename=ChartImages.zip");
    context.Response.Cache.SetNoServerCaching();
    context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);
    using(ZipFile zip = new ZipFile())
    {
        zip.AddFile(context.Server.MapPath("sample1.png"));
        zip.Save(context.Response.OutputStream);
    }
    context.ApplicationInstance.CompleteRequest();
}
هل كانت مفيدة؟

المحلول

يستبدل Response.End مع HttpContext.Current.ApplicationInstance.CompleteRequest

جرب هذه النسخة المختصرة:

Response.Clear();
Response.BufferOutput = false;

Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
using(ZipFile zip = new ZipFile())
{
  zip.AddFile(Server.MapPath("sample1.png"));
  zip.Save(Response.OutputStream);
}
HttpContext.Current.ApplicationInstance.CompleteRequest();

في حالة الفشل في ذلك، استخدم Microsoft Fiddler لمعرفة الأخطاء الأخرى التي قد تحدث.

نصائح أخرى

يجب عليك إنشاء معالج ASHX من أجل هذا.هل حاولت استخدام نوع محتوى "application/zip" بدلاً من ذلك؟

بدلاً من Response.ClearHeaders()، قم بإجراء كامل الاستجابة.مسح (), ، وبعد ذلك، قم بإجراء الاستجابة. النهاية ()

لقد واجهت للتو نفس المشكلة (والإصلاح) شكرًا.

إحدى النقاط التي قد تساعد الباحثين في المستقبل هي أن المشكلة ظهرت بالنسبة لي فقط على مواقع HTTPS.تم تشغيل الكود بشكل جيد على خادم HTTP المحلي الخاص بي.

أعتقد أنه مع HTTPS لن يتم تخزينه مؤقتًا على أي حال لذا يمكن تضمينه في شرط "if(Request.IsSecureConnection)".

لم أستخدم فئة ZipFile مطلقًا، وهذا ما يقال عندما أرسل ملفات أستخدم Response.BinaryWrite()

//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("content-disposition", "attachment;filename=\"" + currentDocument.Name + "\"");



//currentDocument.Document is the byte[] of the file
context.Response.BinaryWrite(currentDocument.Document);

context.Response.End();

لقد واجهت نفس المشكلة وتمكنت من إصلاحها

استجابة. واضح ()؛Response.BufferOutput = false;

                    Response.ContentType = "application/zip";
                    //Response.AddHeader("content-disposition", "inline; filename=\"" + ArchiveName + "\"");
                    Response.AddHeader("content-disposition", "attachment; filename=\"" + ArchiveName + "\"");
                    zipFile.Save(Response.OutputStream);
                   // Response.Close();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    Response.Clear();
                    Response.End();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top