2012-01-09 7 views
1

ダウンロードとダウンロードの2つのページを持つコントローラがあります。サービスからバイト[]を取得し、Response.BinaryWriteを使用して保存しますファイル。この問題は、Response.Clearを使用すると、ダウンロードページがレンダリングから停止されますが、ファイルが正常にダウンロードされることが原因です。ファイルをダウンロードしてページをレンダリングする方法はありますか?方法:レンダリングからページを停止せずにWebサービスからファイルを保存する

私はMVCを使用して、私は私のビューの戻り値の型としてのActionResultとFileResultを使用することだということを認識していますC#の

.NET 4、ASP、モノレール城を、使用しています、私は、しかし、使用に制限されていますモノレール城はそれが既存のプロジェクトであるため、私は最近所有権を持ち、その技術を変えることに重大さはありません。以下は

は、ダウンロードページ

<table> 
    <tr> 
     <td>Your file has been downloaded successfully</td> 
    </tr> 
</table> 

答えて

1

答えは、私が好きだろうほど汎用的ではない、まだこのソリューションは機能しています。ここで

public class MyController : Controller 
{ 
    public void Index() 
    { 
     PropertyBag["includeZeroBalances"] = false; 
     PropertyBag["toDate"] = DateTime.Today.ToShortDateString(); 
    } 

    public void Download(bool includeZeroBalances, DateTime toDate) 
    { 
     MyProxy proxy = GetProxy(); 
     byte[] file = proxy.GetFile(includeZeroBalance, toDate); 
     PropertyBag["downloadurl"] = "data:application/zip;base64," + System.Convert.ToBase64String(file); 
    } 
} 

インデックスページはこちら

${Form.FormTag({@action: 'Download'})} 
    <table> 
     <tr> 
      <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td> 
      <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td> 
     </tr> 
     <tr> 
      <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td> 
      <td>${Form.CheckboxField("includeZeroBalances")}</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>${Form.Submit("Download", {@class: 'submitb'})}</td> 
     </tr> 
    </table> 
${Form.EndFormTag()} 

あるダウンロードページには、あなたはまた、新しいウィンドウでファイルをダウンロードして自動化するJavaScriptを追加することができ

<table> 
    <tr> 
     <td>Your file has been generated successfully. <a href="${downloadurl}">Click here</a> to download your file</td> 
    </tr> 
</table> 

です。ファイルを保存するには、この方法を使用しての

document.Ready(function() 
{ 
    window.open("${downloadurl}"); 
}); 

利点: んファイルがサーバー上に保存されていません。 約300kbを超えてファイルをダウンロードするときのオーバーヘッドが少し下がります レスポンスデータを上書きしないため、ビューは通常通り表示されます。

この方法を使用してファイルを保存する際の短所: 任意のバージョンの任意のブラウザで使用するには十分ではありません。静的ファイルのために使用した場合 、ときに、ファイルの変更、URIは、私は、これが同じ問題に引っかかってきたように見える他の人に役立ちます願っていたファイルの内容はURI

中に埋め込まれているので、再度生成する必要があります。私は、このタイプの質問を多数のボードにわたって、真っ直ぐな解決策なしで見てきました。

1

だろうこれを行うための '正しい' やり方インデックスページここ

${Form.FormTag({@action: 'Download'})} 
    <table> 
     <tr> 
      <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td> 
      <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td> 
     </tr> 
     <tr> 
      <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td> 
      <td>${Form.CheckboxField("includeZeroBalances")}</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>${Form.Submit("Download", {@class: 'submitb'})}</td> 
     </tr> 
    </table> 
${Form.EndFormTag()} 

私のコード例ここで

public class MyController : Controller 
{ 
    public void Index() 
    { 
     PropertyBag["includeZeroBalances"] = false; 
     PropertyBag["toDate"] = DateTime.Today.ToShortDateString(); 
    } 

    public void Download(bool includeZeroBalances, DateTime toDate) 
    { 
     MyProxy proxy = GetProxy(); 
     byte[] file = proxy.GetFile(includeZeroBalance, toDate); 
     Response.Clear(); 
     Response.ContentType = "application/zip"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename="TestFileName.zip"); 
     Response.BinaryWrite(file); 
    } 
} 

されています複数の応答を使用して、残念ながらsome browsers don't support them

あなたができることは、sourceforgeや他のサイトのようなメタリフレッシュを使用することです。代替案については、これらの質問を参照してください。私が思いついた

+0

こんにちはマウリシオ、あなたが提供したリンクと洞察力に感謝します。残念ながら、これは私が探していたものではありませんでした。 –

+0

@ROFFELPOMP:まあ、それはまさにあなたの答えでやったことです。 –

関連する問題