2012-04-04 16 views
3

IEでのファイルをダウンロードするダウンロードハンドラがありますが、Chromeでそれ自体をダウンロードしようとしています。これによって私は、Chromeは、ハンドラのコードがハンドラは、この中で使用されているコードASP.NETダウンロードHandlerはIEでは動作しますがChromeでは動作しません

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

を/ octet-stream "; context.Response.ContentType = Net.Mime.MediaTypeNames.Application.Octet これを使用する習慣をつくると、時間がかかるので誤植の原因にはなりません。 – JDC

答えて

5

「インライン」としてContent-Dispositionヘッダーから「インライン」を除去&「取り付け」を一緒に使用することを想定していない試してください。私は context.Response.ContentType =「アプリケーションを変更することになる

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

感謝の男、魅力的なように働いた – tuckerjt07

関連する問題