fileupload.c
上传用户:coffee44
上传日期:2018-10-23
资源大小:12304k
文件大小:2k
源码类别:

TAPI编程

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  *                                  _   _ ____  _
  3.  *  Project                     ___| | | |  _ | |
  4.  *                             / __| | | | |_) | |
  5.  *                            | (__| |_| |  _ <| |___
  6.  *                             ___|___/|_| ______|
  7.  *
  8.  * $Id: fileupload.c,v 1.5 2008-09-10 07:11:45 danf Exp $
  9.  */
  10. #include <stdio.h>
  11. #include <curl/curl.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. int main(void)
  15. {
  16.   CURL *curl;
  17.   CURLcode res;
  18.   struct stat file_info;
  19.   double speed_upload, total_time;
  20.   FILE *fd;
  21.   fd = fopen("debugit", "rb"); /* open file to upload */
  22.   if(!fd) {
  23.     return 1; /* can't continue */
  24.   }
  25.   /* to get the file size */
  26.   if(fstat(fileno(fd), &file_info) != 0) {
  27.     return 1; /* can't continue */
  28.   }
  29.   curl = curl_easy_init();
  30.   if(curl) {
  31.     /* upload to this place */
  32.     curl_easy_setopt(curl, CURLOPT_URL,
  33.                      "file:///home/dast/src/curl/debug/new");
  34.     /* tell it to "upload" to the URL */
  35.     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  36.     /* set where to read from (on Windows you need to use READFUNCTION too) */
  37.     curl_easy_setopt(curl, CURLOPT_READDATA, fd);
  38.     /* and give the size of the upload (optional) */
  39.     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  40.                      (curl_off_t)file_info.st_size);
  41.     /* enable verbose for easier tracing */
  42.     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  43.     res = curl_easy_perform(curl);
  44.     /* now extract transfer info */
  45.     curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
  46.     curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
  47.     fprintf(stderr, "Speed: %.3f bytes/sec during %.3f secondsn",
  48.             speed_upload, total_time);
  49.     /* always cleanup */
  50.     curl_easy_cleanup(curl);
  51.   }
  52.   return 0;
  53. }