Can someone help me? I am doing an application in MVC 4 and I have a PDF document in a folder which is in my solution explorer. I added a link to call the download method to download that file but when I click the link I get an error that says "Could not find a part of the path 'D:\dev\ScriptManager\Base\Velociti.ScriptEngine\Velociti.ScriptEngine\files\'." The code below is sitting in a class.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Velociti.ScriptEngine public class DownloadResult : ActionResult public DownloadResult() public DownloadResult( string virtualPath) this .VirtualPath = virtualPath; public string VirtualPath { get ; set ; } public string FileDownloadName { get ; set ; } public override void ExecuteResult(ControllerContext context) if (!String.IsNullOrEmpty(FileDownloadName)) context.HttpContext.Response.AddHeader( " content-disposition" , " attachment; filename=" + this .FileDownloadName); string filePath = context.HttpContext.Server.MapPath( this .VirtualPath); context.HttpContext.Response.TransmitFile(filePath); Below is the code sitting in my controller
public ActionResult Download( string name) return new DownloadResult { VirtualPath = " ~/files/" + name, FileDownloadName = name }; Your issue is that you never appear to combine the path with the file name from what I can see.
As a result here:
string filePath = context.HttpContext.Server.MapPath( this .VirtualPath); context.HttpContext.Response.TransmitFile(filePath);
You are just returning the folder path and not the path to the file.
Try changing it to:
string filePath = context.HttpContext.Server.MapPath( this .VirtualPath); context.HttpContext.Response.TransmitFile(Path.Combine(filePath, FileDownloadName));
Hi Pheonyx, I changed my code a bit on the virtual path section. I ammended the VirtualPath with the name of the project as the file that I am downloading is on a folder in my solution explorer:
public ActionResult Download(string name)
{
return new DownloadResult { VirtualPath = "~/Velociti.ScriptEngine/files/" + name, FileDownloadName = name };

}
When I click on the link to try and download the file I get an error on this line of code below:
string filePath = context.HttpContext.Server.MapPath(this.VirtualPath);
context.HttpContext.Response.TransmitFile(Path.Combine(filePath, FileDownloadName));
The error says:
"Value cannot be null.
Parameter name: path2"
I think it is the FileDownloadName as when I step through my code it is NULL.
when you step into
public ActionResult Download(string name)
{
return new DownloadResult { VirtualPath = "~/Velociti.ScriptEngine/files/" + name, FileDownloadName = name };
}

is the "name parameter set at all? if not then that, I suspect, is part of the issue. The system does not know the name of the file it should be giving access to.
context.HttpContext.Response.TransmitFile(filePath);
context.HttpContext.Response.Flush();
It will be helpful.
Jemmy Fedder
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •