Returning an MVC FileContentResult to download .pdf file from

2

Hi I've search around for this quite a bit but I didn't find a situation that really resembled mine..hope I didn't miss a duplicate somewhere

The Goal: Return a file from a UNC share to the client as a download/open option.

Info: The share is located on a different server than the one hosting the web site. When a corresponding folder name on the menu is clicked, I am able to successfully read from the share (I return the files as a JSON result) and in Jquery I then append list items for each file found in the folder and make the list item ID's the filename. This works great.

When these appended list items are clicked on I pass their ID's (which are the filename, like "thefile.pdf") to the following controller which returns a FileContentResult.

files[0].ToString() below is similar to "\server\folder\"

public ActionResult OpenTheFile(string id)

{

List<string> files = new List<string>();

files.AddRange(Directory.GetFiles(LNFiles.ThePath, id, SearchOption.AllDirectories));

Response.AppendHeader("Content-Disposition", "attachment; filename=" + id + ";");

return File(files[0].ToString(), System.Net.Mime.MediaTypeNames.Application.Pdf, id);

}

And yes the obligatory "it works on my local machine". When deployed to the IIS 7.5 server and I click on the list item I get this YSOD error:

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

I'm impersonating a user with rights to the file share...I'm at a loss, i was thinking something with encoding or screwed up rights? I've tried using a virtual dir instead but alas same issue.

ajax
asp.net-mvc
json
asked on Stack Overflow May 5, 2011 by proggrock • edited Jun 17, 2012 by tereško

3 Answers

3

In my case

changing this:

public ActionResult Download(int id)
    {
        var item = ItemRepo.GetItemById(id);
        string path = Path.Combine(Server.MapPath("~/App_Data/Items"), item.Path);
        return File(path, "application/octetstream", item.Path);
    }

to this:

public ActionResult Download(int id)
    {
        var item = ItemRepo.GetItemById(id);
        string path = Path.Combine(Server.MapPath("~/App_Data/Items"), item.Path);
        return File(new FileStream(path, FileMode.Open), "application/octetstream", item.Path);
    }

has worked. I am putting this here just in case anyone needs.

answered on Stack Overflow Jun 12, 2012 by duru
1

Check out this for a workaround You may want to try a packet capture to see if you are receiving the same issue as documented here: http://forums.asp.net/t/1473379.aspx/1

For your unc path - are you directly referencing \servername\share or are you using a network mapped drive letter?

1

God Bless you : ProgRockCode.

and since that involved an ActionResult, I wrote a custom ActionResult that used the "WriteFile" method.

   public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.WriteFile(FilePath, true);
        context.HttpContext.Response.End();
    }
answered on Stack Overflow May 8, 2011 by Srinath M S

User contributions licensed under CC BY-SA 3.0