开发者

Opening a byte stream file using asp.net mvc

Im my asp.net mvc application I have a

  • enclosing the thumb image of the file in an aspx page loaded in an iframe. I want to open the file with an Open/Save dialogbox. The file is uploaded to the database in image datatype. My aspx page has the following html in it:

    <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>
    

    The jquery part is as follows

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

    And the function开发者_高级运维 in the controller is

     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);
        }
    

    I am not able open the file. Can anyone help me on this?


    You cannot use ajax to stream file content to the browser and expect to be prompted with a file open/save dialog. Instead of the call to $.post, try

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

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜