我想上传一个独立于大小的文件。我使用“可恢复”选项。下面的代码适用于文本文件,但是当我使用pdf文件或其他扩展名(jpg,7z)时,我总是遇到503错误。此外,如果我想通过块发送,我收到代码308。 此外,是否可以将用户元数据添加为“key = value”。
// Get Buffer
pInputName = fopen (FILE_PATH,"rb");
if (pInputName==NULL) {
perror ("Error opening file");
return (-1);
}
else
{
resultat = fseek(pInputName, 0, SEEK_END);
filesize= (size_t) ftell(pInputName);
rewind(pInputName);
buffer = (char *) calloc(filesize+1, sizeof(char));
resultat = fread (buffer,sizeof(char),filesize,pInputName);
fclose (pInputName);
}
// GET LOCATION
sprintf_s(tmp, 100, "Authorization: Bearer %s",auth);
headerList = curl_slist_append(headerList, tmp);
curl_easy_reset(curl);
curl_easy_setopt( curl, CURLOPT_URL, "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable") ;
curl_easy_setopt( curl, CURLOPT_POST, 1 ) ;
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, titre ) ;
curl_easy_setopt( curl, CURLOPT_HEADER, 1 ) ;
sprintf_s(tmp,100,"Content-Length: %d",size_titre);
headerList = curl_slist_append(headerList, tmp);
headerList = curl_slist_append(headerList, "Content-Type: application/json; charset=UTF-8");
headerList = curl_slist_append(headerList, "X-Upload-Content-Type: text/plain");
sprintf_s(tmp,100,"X-Upload-Content-Length: %d",filesize);
headerList = curl_slist_append(headerList, tmp);
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headerList) ;
curl_easy_setopt( curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &writeMemory);
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_perform(curl);
value = getValue(&writeMemory, "Location: ", ',');
// PUT Request
curl_slist_free_all(headerList);
headerList = NULL;
curl_easy_reset(curl);
curl_easy_setopt( curl, CURLOPT_URL, value) ;
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, buffer ) ;
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, strlen(buffer) ) ;
sprintf_s(tmp, 100, "Authorization: Bearer %s",auth);
headerList = curl_slist_append(headerList, tmp);
sprintf_s(tmp,100,"Content-Length: %d",filesize);
headerList = curl_slist_append(headerList, tmp);
headerList = curl_slist_append(headerList, "Content-Type: application/pdf");
sprintf_s(tmp, 100, "Content-Range: bytes */%d", filesize);
headerList = curl_slist_append(headerList, tmp);
curl_easy_setopt( curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &writeMemoryPut);
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt( curl, CURLOPT_WRITEHEADER, &writeMemory);
curl_easy_setopt( curl, CURLOPT_HEADERFUNCTION, WriteMemoryCallback);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_perform(curl);