使用asp.net mvc打开字节流文件

时间:2011-08-10 06:53:11

标签: file asp.net-mvc-2 filecontentresult

我的asp.net mvc应用程序我有一个

  • 将文件的拇指图像包含在iframe中加载的aspx页面中。我想用打开/保存对话框打开文件。该文件以image数据类型上载到数据库。 我的aspx页面中包含以下html:

    <li class="thumpimage">
                            <%=Html.Hidden("attachmtId", item.ILDAttachmentId) %>
                            <img src="<%=imgurl %>" alt="test" height="81" width="76" />
                            <span class="thumb_descrp">
                                <%=item.ILDAttachmentName %></span></li>
    

    jquery部分如下

    $(document).ready(function() {
    
            $(".thumpimage").click(function() {
                var attchmtId = $("#attachmtId").val();
                alert(attchmtId);
                $.post('/Instruction/OpenInstnDoc', { attchId: attchmtId });
            });
        });
    

    控制器中的功能是

     public ActionResult OpenInstnDoc(int attchId)
        {
    
            Attachment objAttach = new Attachment();
            objAttach = objAttach.GetAttachmentById(attchId);
    
            byte[] theData = objAttach.BinaryFile;
            Response.AddHeader("content-length", theData.Length.ToString());
            Response.AddHeader("content-disposition", "inline; filename=" + objAttach.AttachmentName + "");
            return File(theData, objAttach.MineType);
        }
    

    我无法打开文件。任何人都可以帮我吗?

  • 1 个答案:

    答案 0 :(得分:0)

    您不能使用ajax将文件内容流式传输到浏览器,并希望通过文件打开/保存对话框提示。而不是调用$ .post,请尝试

    $(document).ready(function() {
    
        $(".thumpimage").click(function() {
            var attchmtId = $("#attachmtId").val();
            alert(attchmtId);
            //$.post('/Instruction/OpenInstnDoc', { attchId: attchmtId });
            window.location.href = "/Instruction/OpenInstnDoc/" + attchmtId;
        });
    });