我的文件上传在ASP MVC3网站上运行良好。目前,文件保存在名为“Files”的网站上的文件夹中。用户可以上传任何类型的文件(例如myphoto.jpg,mydocument.docx等)。
当用户上传文件时,我将有关该文件的信息存储在SQL数据库中,并将其上载等。
我的问题:
答案 0 :(得分:5)
您可以添加控制器来为文件服务。
public class FileController : Controller
{
private IFileStore _fileStore;
public FileController(IFileStrore fileStore)
{
this._fileStore = fileStore;
}
public ActionResult Index(string fileName)
{
// Do a database look up if the user has permission
if (_fileStore.HasPermission(fileName, User))
{
// Flush the file content if the user has permission
var myfile = _fileStore.GetFile(fileName);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.FileName);
Response.AddHeader("Content-Length", myfile.Length.ToString());
Response.ContentType = myfile.Extension.ToLower();
Response.WriteFile(myfile.FullName, false);
Response.Flush();
Response.Close();
}
else
{
// Return a Access Denied page
return View("NoPermission");
}
}
}
答案 1 :(得分:0)
您可以在web.config中设置它:
<!-- This section gives the authenticated user with myRole role access to all of the files that are stored in the images folder. -->
<location path="images">
<system.web>
<authorization>
<allow users =".\myRole" />
</authorization>
</system.web>
</location>