返回MVC FileContentResult从.pdf文件下载.pdf文件

时间:2022-10-01 21:22:04

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.

目标:将文件从UNC共享返回到客户端作为下载/打开选项。

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.

信息:共享位于与托管网站的服务器不同的服务器上。当单击菜单上的相应文件夹名称时,我能够从共享中成功读取(我将文件作为JSON结果返回),然后在Jquery中,我为文件夹中找到的每个文件附加列表项并制作列表项ID是文件名。这很好用。

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.

当单击这些附加的列表项时,我将它们的ID(它们是文件名,如“thefile.pdf”)传递给后面的控制器,该控制器返回FileContentResult。

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

下面的文件[0] .ToString()类似于“\ 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:

是的,必须“它在我的本地机器上工作”。当部署到IIS 7.5服务器并单击列表项时,我收到此YSOD错误:

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

句柄无效。 (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.

我模仿拥有文件共享权限的用户......我不知所措,我在考虑使用编码或搞砸权利?我尝试使用虚拟目录,但同样的问题。

3 个解决方案

#1


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

查看此解决方法您可能需要尝试数据包捕获,以查看是否收到与此处记录的相同的问题: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?

对于您的unc路径 - 您是直接引用\ servername \ share还是使用网络映射的驱动器盘符?

#2


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.

工作。我把它放在这里以防万一有需要。

#3


1  

God Bless you : ProgRockCode.

上帝保佑你:ProgRockCode。

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

因为涉及ActionResult,我编写了一个使用“WriteFile”方法的自定义ActionResult。

   public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.WriteFile(FilePath, true);
        context.HttpContext.Response.End();
    }

#1


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

查看此解决方法您可能需要尝试数据包捕获,以查看是否收到与此处记录的相同的问题: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?

对于您的unc路径 - 您是直接引用\ servername \ share还是使用网络映射的驱动器盘符?

#2


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.

工作。我把它放在这里以防万一有需要。

#3


1  

God Bless you : ProgRockCode.

上帝保佑你:ProgRockCode。

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

因为涉及ActionResult,我编写了一个使用“WriteFile”方法的自定义ActionResult。

   public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.WriteFile(FilePath, true);
        context.HttpContext.Response.End();
    }