ASP MVC如何为图像/文件/视频输出我的根文件夹? [英] asp mvc how to serve image/file/video out of my root folder?

查看:242
本文介绍了ASP MVC如何为图像/文件/视频输出我的根文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库后端上Asp.Net MVC4巫存储文件名Y它路径。例如:

I have a Backend on Asp.Net MVC4 witch in a database stores a "filename" y it "Path". Example:

FileName        Path
AAAA           V:\Clientes\FileName1.pdf
BBBB           V:\Clientes\FileName2.pdf
DDDD           V:\Clientes\FileName3.pdf
CCCC           V:\Clientes\FileName4.pdf

事实上,I'm要消耗从科尔多瓦应用此响应(JSON),我需要theese文件与我aplication送达例如用这样的网址:

The fact is that I´m going to consume this response (JSON) from a cordova application, and I need theese files to be served with my aplication for example with a url like this:

www.myawesomeapp.com/files/FileName1.pdf

如何实现这一目标?他们不想将上传任何东西,唯一要说到后端,其中在服务器(映射驱动器)都存储在文件,仅此而已,但这并不是有效的,以打开一个客户端这个文件。

How to achieve this? They don´t want to upload anything, the only want to say to the backend where in the server ( mapped drive ) are stored the files, nothing more, but this is not valid in order to open this files on a client side.

任何帮助或线索?在此先感谢

Any help or clue? thanks in advance

推荐答案

一般来说,在这种情况下,你会创建一个控制器和一系列动作方法将文件提供给用户。这可以让你保持控制的URL格式和路径,并允许您在申请文件的用户进行安全性和授权检查(如果他们不公开)。

Typically in this scenario, you would create a controller and set of action methods to deliver the files to the user. This allows you to keep control over the URL formatting and path and allows you to perform security and authorization checks on the user requesting the file (if they are not public).

public class FilesController : Controller
{
     //I guess you would want to lookup by filename?
     public FileResult Index(string filename ){
            var fileToReturn = new FileStream("full path to file", FileMode.Open);
            return File(fileToReturn, "content-disposition", "fileName.extension");
     }
}

已用作示例的URL现在应返回FileName1.pdf。事实上,根据您的表往上顶,网址可能是沿着 http://www.myawesomeapp.com/Files/AAAA 线的东西。有这种方法的内部一些工作你仍然需要做的文件名参数转换(或ID,或无论你您的文件中查找)到你应该检索什么实际的文件。

The URL you have used as an example should now return the FileName1.pdf. Actually, based on your "table" up top, the url might be something along the lines of http://www.myawesomeapp.com/Files/AAAA. There is some work inside of this method you will still need to do to translate the filename parameter (or an id or however you look up your files) into what actual file you should be retrieving.

这篇关于ASP MVC如何为图像/文件/视频输出我的根文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆