Click here to Skip to main content
13,660,195 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 };
}
Posted 28-Jun-13 4:54am
Updated 28-Jun-13 5:06am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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));
  Permalink  
Comments
P.C Shabangu 1-Jul-13 2:13am
   
Hi, when i changed the code I got an error that says "The name 'Path' does not exist in the current context"
Pheonyx 1-Jul-13 3:07am
   
Have you referenced the correct assemblies? Path is a static class in .Net
http://msdn.microsoft.com/en-us/library/system.io.path.aspx[^]
P.C Shabangu 1-Jul-13 6:08am
   
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.
Pheonyx 1-Jul-13 6:16am
   
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.
P.C Shabangu 1-Jul-13 7:51am
   
Ok I understand. In my case the pdf document that I want to download is in a folder in my solution explorer. Will it work if I take the name of the document from the folder?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

You have to buffered output.

Put below line after

context.HttpContext.Response.TransmitFile(filePath);

context.HttpContext.Response.Flush();

It will be helpful.

Jemmy Fedder
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 11 Mar 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100