2016-04-08 35 views
1

こんにちは私はサーバーからファイルをダウンロードしようとしています。ファイルをダウンロード詳細ビューからMVC 5

は、ここに私のコントローラ

public ActionResult Downloads() 
    { 
     var dir = new System.IO.DirectoryInfo(Server.MapPath("~/Content/AnnFiles/")); 
     System.IO.FileInfo[] fileNames = dir.GetFiles("*.*"); List<string> items = new List<string>(); 
     foreach (var file in fileNames) 
     { 
      items.Add(file.Name); 
     } 
     return View(items); 
    } 

    public FileResult Download(string file) 
    { 

     var FileVirtualPath = "~/Content/AnnFiles/" + file; 
     return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath)); 
    } 

で、私の見解

@Html.ActionLink("Download", "Download", "announcement", new { id = Model.file}) 

にそれは動作しません。エラーを返します

Could not find a part of the path 'C:\Myprojects\MyprojectName\MyprojectName\Content\AnnFiles\' 

ご存知ですか? ありがとう

+2

あなたは "ファイル" を使用する必要がありながら、あなたは、 "ID" パラメータを渡しますダウンロードアクション – Azargoth

+0

にありがとう、私はそれを@ Html.ActionLink( "ダウンロード"、 "ダウンロード"、 "アナウンス" Model.file)。しかし、それは同じエラーを返します。 Firebugでデバッグすると、リンクがDownload touinta

+1

と表示されます。ancorヘルパーは、 "@ Html.ActionLink(" Download "、" Download "、" announcement "、new {file = Model.file})のようにする必要があります。 "/announcement/Download?file=[yourModel.File"としてレンダリングする必要があります。 –

答えて

2

次のオーバーロードを使用する必要があります。最後にnullパラメータを追加する

@Html.ActionLink("Download", "Download", "announcement", new { file = Model.file}, null); 

は、以下を使用します。

ActionLinkので

LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, RouteValueDictionary, IDictionary)

Fiddle

+0

ありがとうございました! – touinta

+0

@touinta問題ありません、ありがとう。 :) – hutchonoid

関連する問題