2017-08-10 80 views
0

uploading images之后,我想要显示的是image。当图像的扩展名为.jpg时,它正在正常工作。但是当我上传pdf文件时,它会被上传,但不会显示文件。显示图像在jquery中无法正常工作

JQUERY

function setImagesSitePlot(JsonObject) { 
for (i = 0; i < JsonObject.length; i++) { 
    var obj = JsonObject[i].Filename; 
    var obj2 = "ImgSitePlotManual"; 
    var obj3 = JsonObject[i].FileType; 
    var dataImageName = JsonObject[i].ImageName; 

    var ImgObj = parent.document.getElementById(obj2); 
    ImgObj.src = SharedFilePath + obj; 
    $(ImgObj).attr("data-imagename", dataImageName); 
} 

}

HTML

<img width="500" class="img-responsive" id="ImgSitePlotManual" data-imagename="" style="width: 606px;" /> 
+0

这是可能的。我的意思是一个图像标签如何显示PDF文件的预览? –

+0

@KiranDash:是的,我知道。那么,我应该怎么做才能让条件 – BNN

回答

2

你要么必须使用图像代表你在你的JSON字符串一旦返回PDF文件图标上传的文件是pdf

也可以显示在object元素的PDF文件:

<object data='http://website.com/nameoffolder/documentname.pdf#toolbar=1' 
     type='application/pdf' 
     width='100%' 
     height='700px' id='PdfSitePlotManual'> 

你不能在图像中查看PDF文件。

。利用您已经返回fileType属性:

for (i = 0; i < JsonObject.length; i++) { 
    var obj = JsonObject[i].Filename; 
    var obj3 = JsonObject[i].FileType; 
    var dataImageName = JsonObject[i].ImageName; 

    if(obj3 == 'img') 
    { 
     var ImgObj = parent.document.getElementById("ImgSitePlotManual"); 
     ImgObj.src = SharedFilePath + obj; 
     $(ImgObj).attr("data-imagename", dataImageName); 
    } 

    else if(obj3 == 'pdf') 
    { 
     var PdfObj = parent.document.getElementById("PdfSitePlotManual"); 
     PdfObj.setAttribute('data', SharedFilePath + obj); 
    } 
} 
+0

感谢KAD,但如果我想根据用户的选择显示任何东西。我应该使用什么标签,而不是 – BNN

+0

当你上传文件时,你可以识别扩展名和mimetype,然后根据这个返回json响应..这可以通过选择 – KAD

+0

是的,你能告诉我该怎么做。如果你想要我也会发布json – BNN