Opening a byte stream file using asp.net mvc
Im my asp.net mvc application I have a
<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;
});
});
精彩评论