在Salesforce Apex中,如何提供二进制文件

时间:2013-08-08 14:58:15

标签: web-services salesforce apex-code

我有一个VF页面,基本上是我们CMS系统的视图。它使用通用ID登录,并且Web服务能够获取并显示文件列表。现在我希望能够点击一个文件并在新窗口中打开它。我有一个CMS Web服务,它返回文件的字节流。如何设置Apex远程操作以将该流返回到浏览器?此外,如何在新标签中打开它?

我想要删除操作,例如:

public static HttpResponse sendRequest(String endpoint, String method)
{
    HttpRequest req = new HttpRequest();
    req.setMethod(method);
    req.setEndpoint(endpoint);

    HttpResponse resp;
    if (Test.isRunningTest())
    {
        resp = new HttpResponse();
        resp.setBody('');
    }
    else
    {
        Http http = new Http();
        resp = http.send(req);
    }

    return resp;
}


@RemoteAction
public static HttpResponse open(docid)
{
    return sendRequest(API_DOMAIN + '/ws/open/' + docid + '/', 'GET');
}

然后在UI方面:

MainBehavior.Open= function(docid) {
    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.ResourceCenterController.open}',
                    docid,
        function(result, event)
        {
            if (event.status)
            {
                 return result;
            } 
            else if (event.type === 'exception') 
            {
                alert('Not able to open!\n\n' + event.message);
            }
            else
            {
                alert('Not able to open!');
            }
        }
    );

稍后会发生点击事件:

window.open(MainBehavior.Open(docid), '_blank');

我知道这段代码不能正常工作,但这是我的目标。关于如何解决这个问题的任何建议?

1 个答案:

答案 0 :(得分:-1)

if (event.status) {
 // window.open(result,'_blank'); //file in new window
 window.location.href =result;    //file in same window
}

此行将打开文件。避免在新窗口中打开。 AdBlockers将阻止在大多数浏览器中通过JS打开的新窗口。

相关问题