我能够在IE和Chrome浏览器中处理.pdf文件。但在IE中,图像类型文件出现以下错误。
下面是我的对话框函数。
<script type="text/javascript">
function Downloadfile(x) {
var jquery = $;
var empCode = $("#EmpCode").val();
var fileName = $(x).closest("tr").find('td:eq(0)').text();
var actualfileName = $(x).closest("tr").find('td:eq(1)').text();
jquery("#dialog").dialog({
modal: true,
title: actualfileName,
width: 950,
height: 550,
open: function () {
jquery('.ui-dialog').css('z-index', 2000); jquery('.ui-widget-overlay').css('z-index', 1500);
jquery('.ui-dialog-titlebar').css({ "background": "white", "border": "none", "text-align": "left", "font-size": "normal" });
jquery('span').css({ "text-align": "left", "font-size": "normal" });
var object = "<object data=\"http://mysitename.com/filepath/{FileName}\" type=\"application/pdf\" width=\"900px\" height=\"500px\">";
object += "If you are unable to view file, you can download from <a href = \"http://mysitename.com/filepath/{FileName}\">here</a>";
object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
object += "</object>";
object = object.replace(/{FileName}/g, "/" + fileName);
jquery("#dialog").html(object);
}
});
}
</script>
发布于 2021-02-11 15:19:58
我修复了这个问题,通过在对话框中检查带有要加载的文件扩展名的文件类型,并添加了一个额外的条件,如果文件是图像类型,如下所示。
我为图片添加了img
标签,为要加载的图片文件的路径添加了src
。
<script type="text/javascript">
function Downloadfile(x) {
var jquery = $;
var empCode = $("#EmpCode").val();
var fileName = $(x).closest("tr").find('td:eq(0)').text();
var actualfileName = $(x).closest("tr").find('td:eq(1)').text();
var validExtensions = ["jpg", "jpeg", "gif", "png"];
var file = fileName.split('.').pop();
jquery("#dialog").dialog({
modal: true,
title: actualfileName,
width: 950,
height: 550,
open: function () {
jquery('.ui-dialog').css('z-index', 2000); jquery('.ui-widget-overlay').css('z-index', 1500);
jquery('.ui-dialog-titlebar').css({ "background": "white", "border": "none", "text-align": "left", "font-size": "normal" });
jquery('span').css({ "text-align": "left", "font-size": "normal" });
var object = "<object data=\"http://mysitename.com/filepath/{FileName}\" type=\"application/pdf\" width=\"900px\" height=\"500px\">";
object += "If you are unable to view file, you can download from <a href = \"http://mysitename.com/filepath/{FileName}\">here</a>";
object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
object += "</object>";
object = object.replace(/{FileName}/g, "/" + fileName);
//For image files
var image = "<img src=\"http://mysitename.com/filepath/{FileName}\" width=\"900px\" height=\"500px\">";
image = image.replace(/{FileName}/g, "/" + fileName);
if (validExtensions.indexOf(file) != -1) {
jquery("#dialog").html(image);
} else {
jquery("#dialog").html(object);
}
}
});
}
</script>
https://stackoverflow.com/questions/66134989
复制相似问题