2010-11-18 13 views
0

私はGoogleから研究したがまだ混乱しているASP.NET MVC2について2つのことを知りたい。私はここではっきりと清潔な答えを見つけることができるといいですね。ファイルのアップロード/ ASP.NET MVC2でのサンプルのダウンロード?

まず、カスタムファイルパスを使用してサーバーにファイルをアップロードする方法。 (例:/コンテンツ/ファイル)

第2に、urlがURL Rountingを適用したので、そのファイルをダウンロードする方法、それらにマップする方法?

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

答えて

1

アップロードするには、このようなものを使用します。

<form action="/MyController/SaveDocuments/" method="post" enctype="multipart/form-data"> 

     <label for="file1">Document 1</label> 
     <input type="file" id="file1" name="file1" /> 

</form> 

そして、ここにファイルを保存するには、コントローラ上のコードです:ダウンロード用として

 public Document[] SaveDocuments(HttpRequestBase iHttpRequest, Instruction instruction) 
    { 
     List<Document> documents = new List<Document>(); 

     foreach (string inputTagName in iHttpRequest.Files) 
     { 
      HttpPostedFile file = iHttpRequest.Files[inputTagName]; 
      if (file.ContentLength > 0) 
      { 
       if (Path.GetExtension(file.FileName).Length == 0) 
       { 
        throw new ValidationException(string.Format("File '{0}' has no extension (e.g. .doc .pdf)", file.FileName)); 
       } 
       string filePath = documentService.BuildDocumentPath(instruction.InstructionId, file.FileName); 
       file.SaveAs(filePath); 

       documents.Add(new Document 
       { 
        Filename = Path.GetFileName(file.FileName), 
        Path = filePath 
       }); 
      } 
     } 

     return documents.ToArray(); 
    } 

は、 "〜/コンテンツ/ファイル" ディレクトリを持っていると言う...

ますあなたのルートでそれらを除外しなければなりません。

routes.IgnoreRoute("Content/{*pathInfo}"); 
関連する問題