fileupload.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Upload to a file:// URL
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <curl/curl.h>
00028 #include <sys/stat.h>
00029 #include <fcntl.h>
00030 
00031 int main(void)
00032 {
00033   CURL *curl;
00034   CURLcode res;
00035   struct stat file_info;
00036   double speed_upload, total_time;
00037   FILE *fd;
00038 
00039   fd = fopen("debugit", "rb"); /* open file to upload */
00040   if(!fd)
00041     return 1; /* can't continue */
00042 
00043   /* to get the file size */
00044   if(fstat(fileno(fd), &file_info) != 0)
00045     return 1; /* can't continue */
00046 
00047   curl = curl_easy_init();
00048   if(curl) {
00049     /* upload to this place */
00050     curl_easy_setopt(curl, CURLOPT_URL,
00051                      "file:///home/dast/src/curl/debug/new");
00052 
00053     /* tell it to "upload" to the URL */
00054     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00055 
00056     /* set where to read from (on Windows you need to use READFUNCTION too) */
00057     curl_easy_setopt(curl, CURLOPT_READDATA, fd);
00058 
00059     /* and give the size of the upload (optional) */
00060     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00061                      (curl_off_t)file_info.st_size);
00062 
00063     /* enable verbose for easier tracing */
00064     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00065 
00066     res = curl_easy_perform(curl);
00067     /* Check for errors */
00068     if(res != CURLE_OK) {
00069       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00070               curl_easy_strerror(res));
00071 
00072     }
00073     else {
00074       /* now extract transfer info */
00075       curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
00076       curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
00077 
00078       fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
00079               speed_upload, total_time);
00080 
00081     }
00082     /* always cleanup */
00083     curl_easy_cleanup(curl);
00084   }
00085   fclose(fd);
00086   return 0;
00087 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02