2012-04-04 5 views
3

IE에서 파일을 다운로드하는 다운로드 핸들러가 있는데 크롬에서 자체 다운로드하려고 시도합니다. 이것에 의하여 나는 크롬 핸들러의 코드는 핸들러이에 사용되는 코드ASP.NET 다운로드 핸들러는 Chrome에서는 작동하지 않지만 IE에서는 작동합니다.

private static string BuildAbsolute(string relativeUri) 
{ 
    // get current uri 
    Uri uri = HttpContext.Current.Request.Url; 
    // build absolute path 
    string app = HttpContext.Current.Request.ApplicationPath; 
    if (!app.EndsWith("/")) app += "/"; 
    relativeUri = relativeUri.TrimStart('/'); 
    // return the absolute path 
    return HttpUtility.UrlPathEncode(
     String.Format("http://{0}:{1}{2}{3}", 
     uri.Host, uri.Port, app, relativeUri)); 
} 

이 조각로부터 정보를 수신

<%@ WebHandler Language="C#" Class="DownloadHandler" %> 
using System; 
using System.Web; 

public class DownloadHandler : IHttpHandler 
{ 
public void ProcessRequest(HttpContext context) 
{ 
    string file = ""; 

    // get the file name from the querystring 
    if (context.Request.QueryString["Filepath"] != null) 
    { 
     file = context.Request.QueryString["Filepath"].ToString(); 
    } 

    string filename = context.Server.MapPath("~/Minutes/" + file); 
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename); 

    try 
    { 
     if (fileInfo.Exists) 
     { 
      context.Response.Clear(); 
      context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=\"" + fileInfo.Name + "\""); 
      context.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
      context.Response.ContentType = "application/octet-stream"; 
      context.Response.TransmitFile(fileInfo.FullName); 
      context.Response.Flush(); 
     } 
     else 
     { 
      throw new Exception("File not found"); 
     } 
    } 
    catch (Exception ex) 
    { 
     context.Response.ContentType = "text/plain"; 
     context.Response.Write(ex.Message); 
    } 
    finally 
    { 
     context.Response.End(); 
    } 
} 

public bool IsReusable 
{ 
    get 
    { 
     return false; 
    } 
} 



} 

입니다 downloadhandler.ashx라는 파일을 다운로드하려고 시도 의미 방법

public static string ToDownloadMinutes(string fileName) 
{ 
    return BuildAbsolute(String.Format("Handlers/DownloadHandler.ashx?Filepath={0}", fileName)); 
} 

이 다운로드 기능을 Chrome에서 작동시키는 데 도움이되는 모든 사항에 큰 도움이됩니다.

+0

내가 context.Response.ContentType = "응용 프로그램을 변경 것/octet-stream "; 에 의해 context.Response.ContentType = Net.Mime.MediaTypeNames.Application.Octet 이것을 사용하는 습관을 짓는다면 오타가 발생할 수 있습니다. – JDC

답변

5

하는 "인라인"로 내용 - 처리 헤더에서 "인라인"를 제거하십시오 & "첨부 파일"함께 사용할 수 안됩니다

context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" 
    + fileInfo.Name + "\""); 
+0

감사합니다, 매력처럼 일한 사람 – tuckerjt07

관련 문제