共用方式為


從上傳-回復作業擷取回復

BITS Upload-Reply 作業除了將檔案上傳至伺服器之外,也會檢查在伺服器回復時傳送的回復 URL,然後自動遵循回復 URL 並從中下載回應。 如需 BITS-Reply-URL 標頭值的詳細資訊,請參閱 Ack for Fragment 檔。

將作業類型設定為 BG_JOB_TYPE_UPLOAD_REPLY,以建立上傳-回復類型作業。 作業進入BG_JOB_STATE_TRANSFERRED狀態之後,用戶端可以使用回復數據。 若要擷取回復,請呼叫下列其中一種方法:

只有在回復很小且可快速處理,才能在IBackgroundCopyCallback::JobTransferred 方法中呼叫這些方法,以免封鎖回呼線程。 如果您使用 命令行通知 ,而不是回呼,請將作業標識碼傳遞至可執行檔。 可執行檔會使用作業標識碼來呼叫 Complete 方法,讓回覆檔案可供使用。

下列範例示範如何使用每個方法來擷取回復數據。

使用 GetReplyData

下列範例示範如何使用IBackgroundCopyJob2::GetReplyData 方法擷取回復數據。 此範例假設 IBackgroundCopyJob 介面指標有效、作業的類型為 upload-reply,而作業的狀態為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.
}

使用 GetReplyFileName

下列範例示範如何使用IBackgroundCopyJob2::GetReplyFileName 方法擷取回復數據 此範例假設 IBackgroundCopyJob 介面指標有效、作業類型為 upload-reply,而作業的狀態為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.
}