2017-09-21 4 views
0

を使用してDOCXファイルをダウンロードします。私は、このダウンロード機能持つのASP.net Webフォームに

...

ExportData("infoMandat_" + g.NO_MANDAT + ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", g.URL_infoMandat); 

しかし、ファイルは常に空または破損している:

protected void ExportData(string fileName, string fileType, string path) 
    { 
     System.IO.StreamReader sr = new System.IO.StreamReader(path); 


     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
     Response.Charset = ""; 
     Response.ContentType = fileType; 
     Response.Output.Write(sr.ReadToEnd()); 
     Response.Flush(); 
     Response.End(); 

    } 

私はそれを使用します私は、プレーンStreamReader

Aに提案された解決策とそれを読んでいるおそらくので

nswerは関数.Transmit()です。重複としてマークされた質問は、この質問に対する解決策ではありません。

+0

'path'の値は何ですか?このコードと同じマシン上にあるのでしょうか? – Equalsk

+0

DOCXファイルの読み取り中に例外エラーが発生しましたか?もしそうなら、例外エラーのメッセージテキストは何ですか? – JohnH

+0

ボタンクリックで[ASP.NET Excelファイルのダウンロード]の可能な複製(https://stackoverflow.com/questions/6878525/asp-net-excel-file-download-on-button-click) – Legends

答えて

1

ファイルがすでにWebサイトフォルダにある場合は、Streamを使用する必要はありません。 TransmitFileまたはWriteFileのいずれかを使用できます。

pathが正しいフォルダの場所であることを確認してください。たとえば、C:\inetpub\wwwroot\samplewebsite\

protected void ExportData(string fileName, string fileType, string path) 
{ 
    Response.ContentType = fileType; 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 
    Response.TransmitFile(Path.Combine(path + fileName)); 
    Response.End(); 
} 

// Usage 
ExportData("infoMandat_" + g.NO_MANDAT + ".docx", 
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
    g.URL_infoMandat); 
+0

'TransmitFile()'は私が探していたものです。プレーンテキストのときは '.Output.Write()'を使用し、DOCXファイルプレーンテキストではありません...もちろん。どうもありがとう ! –

関連する問題