2009-09-03 7 views
21

Devo creare e restituire il file nella mia aplicazione ASP.net MVC aplication. Il tipo di file dovrebbe essere normale .txt file. So che posso restituire FileResult ma non so come usarlo.Come creare un file e restituirlo tramite FileResult in ASP.NET MVC?

public FilePathResult GetFile() 
{ 
string name = "me.txt"; 

FileInfo info = new FileInfo(name); 
if (!info.Exists) 
{ 
    using (StreamWriter writer = info.CreateText()) 
    { 
     writer.WriteLine("Hello, I am a new text file"); 

    } 
} 

return File(name, "text/plain"); 
} 

Questo codice non funziona. Perché? Come si fa con il risultato del flusso?

risposta

31

EDIT (Se si desidera che il flusso di provare questo:)

public FileStreamResult GetFile() 
{ 
    string name = "me.txt"; 

    FileInfo info = new FileInfo(name); 
    if (!info.Exists) 
    { 
     using (StreamWriter writer = info.CreateText()) 
     { 
      writer.WriteLine("Hello, I am a new text file"); 

     } 
    } 

    return File(info.OpenRead(), "text/plain"); 

} 

È potrebbe provare qualcosa di simile ..

public FilePathResult GetFile() 
{ 
    string name = "me.txt"; 

    FileInfo info = new FileInfo(name); 
    if (!info.Exists) 
    { 
     using (StreamWriter writer = info.CreateText()) 
     { 
      writer.WriteLine("Hello, I am a new text file"); 

     } 
    } 

    return File(name, "text/plain"); 

} 
+3

Considerate anche le altre opzioni- http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc ricordando che File (può ospitare tutti – RichardOD

+0

questo non funziona quindi ho uncked il segno "nike" – Ante

+0

Sì, il file ([parametri], ...) farà quello che vuoi ... devi capire cosa vuoi ... – BigBlondeViking

8

Aprire il file in una StreamReader, e passare il flusso come argomento per il FileResult:

public ActionResult GetFile() 
{ 
    var stream = new StreamReader("thefilepath.txt"); 
    return File(stream.ReadToEnd(), "text/plain"); 
} 
+1

Si noti che ' "thefilepath.txt"' deve essere il percorso completo al file di testo, non solo un percorso relativo. –

+0

come posso crearne uno? con "TextWriter tw = new StreamWriter (" date.txt ");" o qualcos'altro? – Ante

+1

Sì, ad esempio così. Ricorda che i percorsi dei file devono essere percorsi completi e utilizzare istruzioni 'using'. –

1

Un altro esempio di creazione e il download di file da applicazioni ASP NET MVC in una volta, ma il contenuto del file viene creato in memoria (RAM) - al volo:

public ActionResult GetTextFile() 
{ 
    UTF8Encoding encoding = new UTF8Encoding(); 
    byte[] contentAsBytes = encoding.GetBytes("this is text content"); 

    this.HttpContext.Response.ContentType = "text/plain"; 
    this.HttpContext.Response.AddHeader("Content-Disposition", "filename=" + "text.txt"); 
    this.HttpContext.Response.Buffer = true; 
    this.HttpContext.Response.Clear(); 
    this.HttpContext.Response.OutputStream.Write(contentAsBytes, 0, contentAsBytes.Length); 
    this.HttpContext.Response.OutputStream.Flush(); 
    this.HttpContext.Response.End(); 

    return View(); 
} 
Problemi correlati