Provide file download from server

Asked

Viewed 3,072 times

3

There is a feature of registering a customer’s data. The user selects a file from his computer, and this file is imported to the server in a separate folder, and this file can be a PDF, JPEG, PNG.

There is an editing screen of this client, where it will have a button to download this document. My question is: How to implement the functionality of downloading the file stored on the server?

3 answers

6


Thus:

public FileResult Download(int id)
{
    var caminhoDaImagem = /* Aqui você usa id que vem por parâmetro pra fazer alguma operação que seleciona o caminho da imagem de algum lugar */
    byte[] dadosArquivo = System.IO.File.ReadAllBytes(caminhoDaImagem);
    return File(dadosArquivo, System.Net.Mime.MediaTypeNames.Image.Jpeg, "meuarquivo.jpg");
}

See here the MediaTypes for natively available images.

Use:

http://localhost:porta/MeuController/Download/1
  • friend, in the case in the line using (var Fs = new Filestream(wayDaImage)) it asks me as parameter a Filemode, in my case that is to downlaod the correct is to use which? Filemode.Open.?

  • Exactly. I updated the answer.

  • something else, error in Fs.Toarray(), says that filestream does not have a toarray

  • Yes, it would work for MemoryStream, not to FileStream. See my issue.

  • Guy worked perfectly however the file name this coming with the name of my method in the action has as I name the file that is downloading?

  • Yes, look at my edit. The file name is the third argument.

  • 2

    I’d rather this answer than the other for leaving none stream open (even for a short time). But still, I don’t know if it’s really necessary to open the file just for that. + 1 =D

  • 1

    It worked perfectly, thank you very much !

Show 3 more comments

5

If the file exists on the disk and you know its way, it would be better to return it without having to worry about opening it, right?

public FileResult Download(int id) 
{
    string caminho = /*Buscar caminho do arquivo */;
    var result = new FilePathResult(caminho, "image/jpeg")
    {
        FileDownloadName = "nomeParaDownload.jpg"
    };

    return result;
}
  • I understand, but in this case on disk you say on the correct server ? or on the machine of the person who went up the file ?

  • 2

    On the server....

  • Thank you so much for your help !!

4

You may have seen that controller methods that meet page requests return type objects ActionResult.

Normally you return HTML content through the method View controller itself. You can change the return type to a file easily, just return an object FileStreamResult instead of calling the method View.

Follows the official documentation.

And an example:

public ActionResult BoloDeFuba()
{
    FileStream arquivo = new FileStream(@"c:\bolo de fubá.doc");
    FileStreamResult download = new FileStreamResult(arquivo, "application/msword"); // O segundo parâmetro é o Mime type
    download.FileDownloadName = "bolo de fubá.doc";
    return download;
}

Note that the FileStreamResult receives any object of the type Stream. You can mount a file in memory or load from the database instead of loading from a folder when needed or more convenient.

  • 3

    Youthful, FileStream implements IDisposable. Good practice use using not to cause a memory usage spike in the application.

  • @Gypsy omorrisonmendez was worth!

  • 3

    @Ciganomorrisonmendez I was left with a doubt, because close the Stream prevent its correct return by the controller. And in fact, I tested and used the using in this case causes error. In this special case, we do not need to worry, because the method FileStreamResult calls the disposing of stream internally. Source: http://stackoverflow.com/questions/26275764/does-filestreamresult-close-stream

  • True, because FileStreamResult supposes that its connection with the stream will stay open after return. I do not find this approach good for normal file.

  • Thank you so much for your help !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.