i want to add a file to json structure and send it,i want to add that file to json using json array for example i reading file as binary an append to json array(use this code) :
Qt Code:
  1. void send_file(QString file_path)
  2. {
  3. cout<<"SEND FILE: "<<file_path.toStdString()<<"\n";
  4.  
  5. QFile i_file(file_path.remove(file_path.count()-1,1));
  6. i_file.open(QIODevice::ReadOnly);
  7. if (i_file.isOpen())
  8. {
  9. QJsonObject packet;
  10. packet["message_type"]="ANSWER_PACKET";
  11. packet["sender"]=mac_address;
  12. packet["reciver"]="control_panel";
  13. packet["answer_type"]="PRIVATE";
  14. packet["what"]="send_file";
  15. packet["file_type"]="GENERAL";
  16. packet["file_path"]=file_path;
  17. QDataStream i_file_stream(&i_file);
  18. QJsonArray i_array;
  19. while (!i_file.atEnd())
  20. {
  21. int len=1048576;
  22. char char_buffer[len];
  23. int size_read=i_file_stream.readRawData(char_buffer,len);
  24. if (size_read>0)
  25. {
  26. QByteArray temp(char_buffer,size_read);
  27. i_array.append(temp.constData());
  28. }
  29. }
  30.  
  31. packet["file_content"]=i_array;
  32. QJsonDocument json_doc(packet);
  33. QByteArray buffer=json_doc.toJson();
  34.  
  35. cout<<QString(buffer).toStdString()<<"\n";
  36. i_file.close();
  37. send_packet(buffer);
  38.  
  39. }
  40. }
To copy to clipboard, switch view to plain text mode 
now,in another hand i using this code for deserializing and writing file:
Qt Code:
  1. void settings_frame_widget::save_file(QJsonObject packet)
  2. {
  3. QJsonArray i_array =packet["file_content"].toArray();
  4. QByteArray file_buffer;
  5.  
  6. foreach (QJsonValue item, i_array)
  7. {
  8. QByteArray temp=item.toVariant().toByteArray();
  9. file_buffer.append(temp);
  10. }
  11. //Write File:
  12. if (download_address!=QString())
  13. {
  14. QFile i_file(download_address);
  15. i_file.open(QIODevice::WriteOnly);
  16. if (i_file.isOpen())
  17. {
  18. QDataStream file_stream(&i_file);
  19. file_stream<<file_buffer;
  20. }
  21. QMessageBox msgbox;
  22. msgbox.setText("Download Finish!");
  23. msgbox.exec();
  24. i_file.close();
  25. download_address=QString();
  26. }
  27. }
To copy to clipboard, switch view to plain text mode 

for example when i send sequential file like this:
Qt Code:
  1. {
  2. "res1":{
  3. "isError":false,
  4. "key":"1234566",
  5. "data":{
  6. "messages":"O.K!"
  7. }
  8. }
  9. }
To copy to clipboard, switch view to plain text mode 
when recive it and write to file , see a slacks byte in first of file,like this:
slack.jpg
this sudden cause of random access file is damaged...
what's the problem!?