Dela via


Hämtar svaret från ett Upload-Reply-jobb

Ett BITS-Upload-Reply jobb, förutom att ladda upp en fil till en server, kommer också att undersöka en svars-URL som skickas som en del av serversvaret och sedan automatiskt följa svars-URL:en och ladda ned ett svar från den. Mer information om bits-Reply-URL-huvudvärdet finns i dokumentationen Ack for Fragment.

Ange jobbtypen som BG_JOB_TYPE_UPLOAD_REPLY för att skapa ett jobb av typ Upload-Reply. Svarsdata är tillgängliga för klienten efter att jobbet går in i läget BG_JOB_STATE_TRANSFERRED. Om du vill hämta svaret anropar du någon av följande metoder:

Anropa dessa metoder i din IBackgroundCopyCallback::JobTransferred-metoden endast om svaret är litet och kan bearbetas snabbt för att inte blockera callback-tråden. Om du använder kommandoradsavisering i stället för återanropet skickar du jobbidentifieraren till den körbara filen. Den körbara filen använder jobbidentifieraren för att anropa metoden Complete för att göra svarsfilen tillgänglig.

I följande exempel visas hur du använder varje metod för att hämta svarsdata.

Användning av GetReplyData

I följande exempel visas hur du hämtar svarsdata med hjälp av metoden IBackgroundCopyJob2::GetReplyData. Exemplet förutsätter att gränssnittspekaren IBackgroundCopyJob är giltig, jobbet är av typen uppladdningssvar och tillståndet för jobbet är BG_JOB_STATE_TRANSFERRED.

HRESULT hr;
IBackgroundCopyJob* pJob;
IBackgroundCopyJob2* pJob2 = NULL;
BYTE* pReply = NULL;
UINT64 ReplySize;

//Need to query the IBackgroundCopyJob interface for an IBackgroundCopyJob2
//interface pointer. The IBackgroundCopyJob2 interface contains the GetReplyData method.
hr = pJob->QueryInterface(__uuidof(IBackgroundCopyJob2), (void**)&pJob2);
if (SUCCEEDED(hr))
{
    hr = pJob2->GetReplyData(&pReply, &ReplySize);
    if (S_OK == hr))
    {
        if (pReply)
        {
            //Do something with the data.
            CoTaskMemFree(pReply);
        }
        else
        {
            //The server application did not return a reply.
        }
    }
    else if (BG_E_TOO_LARGE == hr)
    {
        //The reply exceeds 1 MB. To retrieve the reply, get the reply file name,
        //complete the job, open the reply file, and read the reply.
    }
    else
    {
        //Handle the error
    }

    pJob2->Release(); //When done, release the interface.
}
else
{
    //Handle error. QueryInterface will return E_NOINTERFACE if the version of BITS
    //running on the computer is less than BITS 1.5.
}

Använda GetReplyFileName

I följande exempel visas hur du hämtar svarsdata med hjälp av metoden IBackgroundCopyJob2::GetReplyFileName. Exemplet förutsätter att IBackgroundCopyJob gränssnittspekaren är giltig, typen av jobb är uppladdning-svar och jobbets tillstånd är BG_JOB_STATE_TRANSFERRED.

HRESULT hr;
IBackgroundCopyJob* pJob;
IBackgroundCopyJob2* pJob2 = NULL;
WCHAR* pszFileName = NULL;

//Need to query the IBackgroundCopyJob interface for an IBackgroundCopyJob2
//interface pointer. The IBackgroundCopyJob2 interface contains the GetReplyFileName method.
hr = pJob->QueryInterface(__uuidof(IBackgroundCopyJob2), (void**)&pJob2);
if (SUCCEEDED(hr))
{
    hr = pJob2->GetReplyFileName(&pszFileName);
    if (SUCCEEDED(hr))
    {
        //Calling the Complete method removes the job from the queue, 
        //so make sure you maintain an interface pointer to this job 
        //or retrieve any job related information that you require 
        //when processing the reply.
        hr = pJob->Complete();

        //Open, read the file, and do something with the data.
        CoTaskMemFree(pszFileName);
    }

    pJob2->Release(); //When done, release the interface.
}
else
{
    //Handle error. QueryInterface will return E_NOINTERFACE if the version of BITS
    //running on the computer is less than BITS 1.5.
}