我在ASP.NET MVC中将存储在数据库中的文件发送回用户时遇到问题.我想要的是一个列出两个链接的视图,一个用于查看文件并让发送给浏览器的mimetype确定应该如何处理,另一个用于强制下载.
如果我选择查看调用的文件SomeRandomFile.bak并且浏览器没有关联的程序来打开这种类型的文件,那么我没有问题,它默认为下载行为.但是,如果我选择查看调用的文件,SomeRandomFile.pdf或者SomeRandomFile.jpg我希望文件只是打开.但是我也希望将下载链接保留在一边,这样无论文件类型如何,我都可以强制下载提示.这有意义吗?
我已经尝试过FileStreamResult它适用于大多数文件,它的构造函数默认不接受文件名,因此根据url(根据内容类型不知道要提供的扩展名)为未知文件分配文件名.如果我通过指定强制文件名,我将失去浏览器直接打开文件的能力,我得到一个下载提示.有人遇到过这种情况么.
这些是我迄今为止尝试过的例子.
//Gives me a download prompt.
return File(document.Data, document.ContentType, document.Name);
Run Code Online (Sandbox Code Playgroud)
//Opens if it is a known extension type, downloads otherwise (download has bogus name and missing extension)
return new FileStreamResult(new MemoryStream(document.Data), document.ContentType);
Run Code Online (Sandbox Code Playgroud)
//Gives me a download prompt (lose the ability to open by default if known type)
return new FileStreamResult(new MemoryStream(document.Data), document.ContentType) {FileDownloadName = document.Name};
Run Code Online (Sandbox Code Playgroud)
有什么建议?
如何强制IIS Express以经典模式运行?我需要这个配置保留.csproj,一旦这个文件说明一个项目应该用IIS Express打开.
我知道如何通过应用程序池设置在完整的IIS中切换到"经典模式"(从"集成模式").但我无法在当前的IIS Developer Express测试版中找到如何做到这一点.
MS Developer Express FAQ说它可以完成,但不是如何完成.据我所知,它不在WebMatrix GUI中.也许自定义Developer Express项目文件,但我根本没有看到任何文档.
仅供参考,有一个设置区域,您可以在其中设置SSL,CLR版本(在本例中为2.0),这是我认为管道模式(经典/集成)的地方,但它不存在.
Visual Studio 2013使用ASP.NET MVC,Owin,WebAPI创建了Web App.在VS 2013中一切运行正常.但是,当我在VS 2012中调试时,我收到错误:"此操作需要IIS集成管道模式."
我不知道如何更改Dev实例的IIS设置.其他类似的解决方案因此不适用.
堆栈跟踪:
[PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.]
System.Web.HttpResponse.get_Headers() +9743542
System.Web.HttpResponseWrapper.get_Headers() +9
Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders..ctor(HttpResponseBase response) +72
Microsoft.Owin.Host.SystemWeb.OwinCallContext.CreateEnvironment() +434
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.GetInitialEnvironment(HttpApplication application) +303
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.PrepareInitialContext(HttpApplication application) +65
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.BeginEvent( Object sender, EventArgs e, AsyncCallback cb, Object extradata) +622
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +285
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Run Code Online (Sandbox Code Playgroud)