3

クエリ文字列を介して渡されたパラメーターに基づいて、Response.WriteFile(fileName) であるリソース ハンドラーがあります。私は MIME タイプを正しく処理していますが、一部のブラウザーでは問題があり、ファイル名は MyPdf.pdf (出力しているファイル) ではなく Res.ashx (ハンドラーの名前) として表示されます。ファイルがサーバーに送り返されたときにファイルの名前を変更する方法を教えてもらえますか? これが私のコードです:

// Get the name of the application
string application = context.Request.QueryString["a"];
string resource = context.Request.QueryString["r"];

// Parse the file extension
string[] extensionArray = resource.Split(".".ToCharArray());

// Set the content type
if (extensionArray.Length > 0)
    context.Response.ContentType = MimeHandler.GetContentType(
        extensionArray[extensionArray.Length - 1].ToLower());

// clean the information
application = (string.IsNullOrEmpty(application)) ?
    "../App_Data/" : application.Replace("..", "");

// clean the resource
resource = (string.IsNullOrEmpty(resource)) ?
    "" : resource.Replace("..", "");

string url = "./App_Data/" + application + "/" + resource;


context.Response.WriteFile(url);
4

3 に答える 3

4

Joel のコメントから拡張すると、実際のコードは次のようになります。

context.Response.AddHeader("content-disposition", "attachment; filename=" + resource);
于 2008-10-30T19:09:03.560 に答える
1

次の Scott Hanselman の投稿が参考になります:
http://www.hanselman.com/blog/CommentView.aspx?guid=360

于 2008-10-30T18:58:43.817 に答える
0

皆さん、答えてくれてありがとう。最終的なコードは機能し、pdf をチェックします。

if (extensionArray[extensionArray.Length - 1].ToLower() == "pdf")
    context.Response.AddHeader("content-disposition", 
        "Attachment; filename=" + resource);
于 2008-10-30T19:18:42.903 に答える