byte[] to File Type in MVC 3

后端 未结 1 1062
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 02:57

In my MVC application, I recently have configured a page to allow an arbitrary file type to be uploaded(with certain restrictions that dont apply to this question).

相关标签:
1条回答
  • 2020-12-21 03:24

    You would return a FileContentResult.

    In you controller, something like this:

     byte[] data = MyDataContext.DocumentTable.First(p => p.DocumentUID == id);
     return File(data, "text/plain", "myfile.txt");
    

    In addition to the extension of the file, you need to specify a name for it as well. The 2nd parameter is the MIME type. This is important for some browsers (like FireFox) to determine which application to open your file with. Firefox will prefer the MIME type over the extension.

    0 讨论(0)
提交回复
热议问题