fileupload.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <curl/curl.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. int main(void)
  27. {
  28. CURL *curl;
  29. CURLcode res;
  30. struct stat file_info;
  31. double speed_upload, total_time;
  32. FILE *fd;
  33. fd = fopen("debugit", "rb"); /* open file to upload */
  34. if(!fd) {
  35. return 1; /* can't continue */
  36. }
  37. /* to get the file size */
  38. if(fstat(fileno(fd), &file_info) != 0) {
  39. return 1; /* can't continue */
  40. }
  41. curl = curl_easy_init();
  42. if(curl) {
  43. /* upload to this place */
  44. curl_easy_setopt(curl, CURLOPT_URL,
  45. "file:///home/dast/src/curl/debug/new");
  46. /* tell it to "upload" to the URL */
  47. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  48. /* set where to read from (on Windows you need to use READFUNCTION too) */
  49. curl_easy_setopt(curl, CURLOPT_READDATA, fd);
  50. /* and give the size of the upload (optional) */
  51. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  52. (curl_off_t)file_info.st_size);
  53. /* enable verbose for easier tracing */
  54. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  55. res = curl_easy_perform(curl);
  56. /* now extract transfer info */
  57. curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
  58. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
  59. fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
  60. speed_upload, total_time);
  61. /* always cleanup */
  62. curl_easy_cleanup(curl);
  63. }
  64. return 0;
  65. }