Salesforce / php - 下载的图像附件已损坏 - 任何想法?

时间:2013-02-08 13:27:10

标签: php web-services salesforce

以下是使用php toolkit和enterprise wsdl:

从Salesforce.com下载附件的代码
    header('Content-Type: application/force-download');
    header('Content-Disposition: inline; filename="image.jpg"');        
    $mySforceConnection = getConnection();
    $query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
    $queryResult = $mySforceConnection->query($query);

    $records = $queryResult->records;
    print_r(base64_decode($records[0]->fields->Body));

当我这样做时,文件会以正确的字节数正确下载,但是当我打开图像时,Windows图像查看器会说它已损坏。知道为什么会这样吗?

相同的代码适用于PDF和文本文件。

1 个答案:

答案 0 :(得分:1)

你真的想要回应输出,正如@eyescream提到的那样。使用print_r函数时,会在输出中放置其他制表符和换行符,以使其更具可读性。普通echo输出正确。

header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="image.jpg"');

$mySforceConnection = getConnection();

$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;
echo base64_decode($records[0]->fields->Body);