Skip to content

How to use this for binary file uploads #881

@tilakhere

Description

@tilakhere

untitled.zip

  • What is the issue you have?
    I am writing a code where the front end sends a file within a json object
    I have file in js["body"] the contents of the file.
    I also have the size of the file in js["size"]
    where js is the json object as

JSON OBJECT IS {
"body": "-\u001e_}x#\u0000\u0000\u0000#B����!",
"filename": "untitled.txt",
"size": 16
}

body is the file content and size is the size of the file in bytes

On printing js["body"] the file contents are correct.
Now I need to send this file via socket to another service.
For that I tried stringifying it, memcpy into char* buffer, memcpy in void* buffer but none seems to work correctly.

What is the correct method to copy js["body"] byte by byte into a char*, so that I may send it over a socket to another service which saves it as a actual file.

How do I do that?

sample code I am using

cout<<"File content is "<<js["body"]<<endl;
cout<<"File content as string is "<<js["body"].get<string>()<<endl;
void *file1 = (unsigned char*)malloc(16);
memcpy(file1, &js["body"], 16);
cout<<"File content as char buffer is "<<file1<<endl;

Output I am getting is

File content is "-\u001e_}x#\u0000\u0000\u0000#B����!"
File content as string is -�_}x##B����!
File content as char buffer is 0x1e4cfc0

Only the first output is correct in this case which I am getting by directly printing js["body"]

Any pointers would be highly appreciated.

ENVIRONMENT
OS : ubuntu 16.04
COMPILER : g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
ATTACHED is the binary file within the zip which I am uploading.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions