单击打开Word文档尝试重新连接到控制器动作,同时下载 [英] Clicking Open Word Document tries to reconnect to Controller action while downloading

查看:139
本文介绍了单击打开Word文档尝试重新连接到控制器动作,同时下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如果可能的话要求从服务器下载一个文件,但打开内联。我目前在做;

I have a requirement to download a file from the server, but open inline if possible. I'm currently doing;

Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", documentFileName));
result = new FileStreamResult(new FileStream(documentFilePath, FileMode.Open), "application/msword");

我已经把应用程序/ msword在那里,现在,因为这是我有一个问题是什么。当我点击word文档打开,就好像文档做多次调用回的动作,但没有会议,没有数据库,以便它崩溃。当用户运行此,他们看到一个长吊死,下载对话框终于出现在词,他们不得不取消它。该文件是存在的,是有效的,但,这是不可取的。

I've put application/msword in there right now, because that's what I'm having a problem with. When I click Open on the word document, it's as if the document makes multiple calls back to the action, but there is no session and no database so it crashes. When the user is running this, they see a long hang, the "Downloading" dialog finally appears in word and they have to cancel it. The document is there and is valid but this is not desirable.

的PDF,PNG图片等下载罚款。任何人都可以解释这种现象,并给我一些提示如何解决?

Pdfs, pngs etc. download fine. Can anybody explain this behavior, and give me some hints as to how I fix it?

更新:

动作基本上看起来像;

The action basically looks like;

[HttpPost]
public FileResult View(int id, int source) 
{
  var document = GetDocumentFromDatabase(id, source);

  documentFilePath = Path.Combine( documentsDirectory, document.Name);
  documentName = document.Name; 

Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", documentFileName));
result = new FileStreamResult(new FileStream(documentFilePath, FileMode.Open), "application/msword");

return result;
}

我修剪下来,因为我不能共享的具体细节,但完整的想法是存在的。

I've trimmed it down, as I can't share the specifics, but the full idea is there.

答:

我有可用的内容类型的查找,在那里我已经定义文件是否是内联或附件,当我发现一个word文档,我将它设置为附件。没有更多的错误。 PDF在浏览器中打开还是因为我将它设置为内联。

I have a lookup of available content-types, in there I have defined whether the file is inline or attachment, and when I detect a word document, I set it to attachment. No more error. PDF opens in the browser still because I set it to inline.

推荐答案

我使用的:

 public ActionResult GetAttachment(int id)
        {

            var attachment = _repository.GetAttachByID(id);
            if (attachment != null)
            {
                Response.AppendHeader("Content-Disposition",string.Format("inline; filename={0}",attachment.FileName));
                return File(attachment.File, attachment.MimeType, attachment.FileName);
            }
            else
            {
                return null;
            }                       
        }

问候

这篇关于单击打开Word文档尝试重新连接到控制器动作,同时下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆