2017-08-01 90 views
-1

下面是我返回的json对象。正如你所看到的,我有一个名为“DocumentVersions”的数组,其中包含要显示的文件的Blob地址。在成功函数上,我想在div下显示图像。我尝试循环但我不知道如何显示图像。我可以有多个文件返回。如何从包含数组的JSON对象显示文件?

{ 
    "FileUploadID":"27", 
    "DocumentVersions":[ 
    { 
    "Active":true, 
    "DocumentVersionID":"5", 
    "FileName":"Logo0112.png", 
    "ContentLength":18846, 
    "ContentType":"image/png",   " 
    "RevisionNumber":0, 
    "RevisionDate":"2017-08-01T12:24:04.7748026+01:00",      
    "Blob":"https://address/documents/75755df4af5f.png",   
    "BlobFileName":75755df4af5f.png"   

    } 
    ], 
    "success":true, 
    "id":"27", 
    "message":"The Files have been uploaded" 
} 

这是我的成功功能。从哪里获得“未定义‘一’不能读取属性”一滴

 myDiv.on("complete", function (data) { 

       res = JSON.parse(data.xhr.responseText); 

       console.log(res); 

       if (res.success == true) { 

        for (var key in res) { 
         var optionhtml = '<p="' + res[key].FileUploadID + 
         '">' + res[key].DocumentVersions.Blob + '</p>'; 
         $(".test").append(optionhtml); 

        }       
       } 
       else { 
        alert(res.message); 
       } 

      }); 
+0

你可以发布你的代码jsfiddle吗? –

回答

0

正如你所看到的,DocumentVersions不是一个对象,它与对象(在这种情况下,只有一个对象)一个数组。

{ 
    "FileUploadID":"27", 
    "DocumentVersions":[ 

    { 
    "Active":true, 
    "DocumentVersionID":"5", 
    "FileName":"Logo0112.png", 
    "ContentLength":18846, 
    "ContentType":"image/png",   " 
    "RevisionNumber":0, 
    "RevisionDate":"2017-08-01T12:24:04.7748026+01:00",      
    "Blob":"https://address/documents/75755df4af5f.png",   
    "BlobFileName":75755df4af5f.png"   

    } 

    ], 
    "success":true, 
    "id":"27", 
    "message":"The Files have been uploaded" 
} 

你需要指定数组中的内部对象,你想要得到的数据:

res[key].DocumentVersions[0].Blob 
+0

我甚至会说你想循环浏览文档版本以获取所有数据。 – JBO

+0

我只是一个不确定的时候,我这样做 – user8392603

0

感谢您的帮助,我用下面的代码这让我得到了解决我的问题图像文件和显示。

     res.DocumentVersions.forEach(function (obj) { 
         var img = new Image(); 
         img.src = obj.Blob; 
         img.name = obj.FileName; 
         img.setAttribute("class", "fileLoad");       
         $("#fileupload").append(img); 
        }); 
相关问题