2016-09-21 14 views
9

Tôi cố tải xuống tệp excel được thực hiện bởi EPPlus.dll từ ứng dụng biểu mẫu web asp.net C#. nhưng tôi bị lỗi - lỗi mạng. Cần lưu ý rằng lỗi được đề cập chỉ xảy ra trong chrome và công việc có thể được thực hiện thành công trong các trình duyệt khác.Không thành công - lỗi mạng khi tải xuống tệp excel do EPPlus.dll thực hiện

bằng cách lỗi này không xảy ra trên máy chủ cục bộ của tôi và nó chỉ xảy ra trên máy chủ chính.

Sẽ rất hữu ích nếu ai đó có thể giải thích giải pháp cho vấn đề này.

http://www.irandnn.ir/blog/PostId/29/epplus

+0

po st mã cho sự kiện nhấp chuột hoặc một cái gì đó mà tập tin này đang được thực sự gửi cho khách hàng. – Rex

Trả lời

5

Hãy thử điều này:

using (ExcelPackage p = new ExcelPackage()) 
{ 
    //Code to fill Excel file with data. 


    Byte[] bin = p.GetAsByteArray(); 

    Response.ClearHeaders(); 
    Response.ClearContent(); 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Nombre_Del_Libro + ".xlsx")); 
    Response.BinaryWrite(bin); 
    Response.Flush(); 
    Response.End(); 
} 
12

tôi đã cùng một vấn đề khi tôi đã sử dụng Response.Clear() và Response.Close() và phải tránh chúng để tìm mã của tôi như sau mà đang làm việc.

Response.Buffer = true; 
Response.ContentType = mimeType; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOfFile); 
Response.BinaryWrite(bytes); 
Response.End(); 
0

tôi đã cùng một vấn đề và tôi giải quyết với mã dưới đây, lưu ý các dấu ngoặc kép trong filename = \ "Name.xlsx \":

Response.Buffer = true; 
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.AppendHeader("content-disposition", "attachment; filename=\"Name.xlsx\""); 
//Writeout the Content 
Response.BinaryWrite(bytes); 
1

Tôi không thích trả lời.End() vì ném một ngoại lệ

protected void DownloadFile(FileInfo downloadFile, string downloadFilename, string downloadContentType) 
    { 
     Byte[] bin = File.ReadAllBytes(downloadFile.FullName); 

     Response.ClearHeaders(); 
     Response.ClearContent(); 
     Response.ContentType = downloadContentType; 
     Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", downloadFilename)); 
     Response.BinaryWrite(bin); 
     Response.Flush(); 
     Response.SuppressContent = true; 
    } 
+1

Điều này giải quyết được vấn đề của tôi. cảm ơn bạn – Sergio

Các vấn đề liên quan