Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. uploadig adobe reader file in local host in c language

uploadig adobe reader file in local host in c language

Scheduled Pinned Locked Moved C / C++ / MFC
helpphpadobetesting
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sunycity
    wrote on last edited by
    #1

    > I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code int main() { static char *filename = "tutorial.pdf"; //Filename to be loaded static char *filepath = "C:\\tutorial.pdf"; //Filename to be loaded `static char *type = "text/pdf";` static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; static char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb"; char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; char *pos; // used in the loop // Open file pFile = fopen(filepath, "rb"); if (pFile == NULL) { printf("ERROR_OPEN_FILE"); getchar(); return ERROR_OPEN_FILE; } printf("OPEN_FILE\n"); // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: content = (char*)malloc(sizeof(char)*lSize); if (content == NULL) { printf("ERROR_MEMORY"); getchar(); return ERROR_OPEN_FILE; } printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize); // copy the file into the buffer: result = fread(content, 1, lSize, pFile); rewind (pFile); if (result != lSize) { printf("ERROR_SIZE"); getchar(); return ERROR_OPEN_FILE; } printf("SIZE_OK\n"); // terminate fclose(pFile); printf("FILE_CLOSE\n"); //allocate memory to contain the whole file + HEADER buffer = (char*)malloc(sizeof(char)*lSize + 2048); //print header sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename); sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type); sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize); sprintf(buffer, "%s\r\n", buffer);

    J 1 Reply Last reply
    0
    • S sunycity

      > I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code int main() { static char *filename = "tutorial.pdf"; //Filename to be loaded static char *filepath = "C:\\tutorial.pdf"; //Filename to be loaded `static char *type = "text/pdf";` static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; static char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb"; char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; char *pos; // used in the loop // Open file pFile = fopen(filepath, "rb"); if (pFile == NULL) { printf("ERROR_OPEN_FILE"); getchar(); return ERROR_OPEN_FILE; } printf("OPEN_FILE\n"); // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: content = (char*)malloc(sizeof(char)*lSize); if (content == NULL) { printf("ERROR_MEMORY"); getchar(); return ERROR_OPEN_FILE; } printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize); // copy the file into the buffer: result = fread(content, 1, lSize, pFile); rewind (pFile); if (result != lSize) { printf("ERROR_SIZE"); getchar(); return ERROR_OPEN_FILE; } printf("SIZE_OK\n"); // terminate fclose(pFile); printf("FILE_CLOSE\n"); //allocate memory to contain the whole file + HEADER buffer = (char*)malloc(sizeof(char)*lSize + 2048); //print header sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename); sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type); sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize); sprintf(buffer, "%s\r\n", buffer);

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      You should not pass the buffer itself as format argument to sprintf. It depends on the implementation (the used standard C library) if it is supported or not. But even if it is supported it is bad style. And in your case it is the reason for the corrupted buffer content here:

      sprintf(buffer, "%s\r\n", buffer);
      memcpy(buffer + strlen(buffer),content,lSize);
      sprintf(buffer, "%s\r\n", buffer);

      The PDF file is a binary file. While you use memcpy to append the binary data to your buffer, the next sprintf call stops at the first null byte truncating the buffer. A common solution is to use a buffer offset variable which is incremented by the return value of sprintf:

      int ofs = strlen(buffer);
      ofs += sprintf(buffer + ofs, "\r\n");
      memcpy(buffer + ofs,content,lSize);
      ofs += lSize;
      ofs += sprintf(buffer + ofs, "\r\n");

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups