0

ページにデータを含むテーブルを表示し、ユーザーが[エクスポート]をクリックすると、データをExcelファイルに入れます。ここで、このファイルを開いて、ユーザーがファイルを保存したり、表示したりできるようにします。これは私が持っているコードです。ユーザーにファイルを保存するように促しますが、そのファイルはページ全体です!! ここで何が間違っているのですか?

   string filename = filePath.Substring(12);
                        System.IO.File.Copy(filePath, "C:/Work/MCTestSuiteCertificate/TS.ToDoViewer/Content/" + filename, true);
                        Response.Clear();
                        Response.ContentType = @"application\xls";
                       // FileInfo file = new FileInfo(Server.MapPath("~/Content/" + filename));
                        Response.AddHeader("Filename", filename);
                        Response.ContentType = "application/xls";
                        Response.TransmitFile("C:/Work/MCTestSuiteCertificate/TS.ToDoViewer/Content/" + filename);
                        //Response.WriteFile(file.FullName);
                        Response.Flush();
4

1 に答える 1

2

メソッドを使用Fileします (これは を返しますFileResult)。ResponseMVC アプリケーションで直接操作しないでください。

public ActionResult YourAction()
{
    ...
    string filename = filePath.Substring(12);
    return File(Path.Combine(@"C:\Work\MCTestSuiteCertificate\TS.ToDoViewer\Content", filename), "application/xls");
}

Path.CombinePS また、文字列連結の代わりに を使用することに注意してください。

于 2012-08-16T08:07:15.210 に答える