locked
Replacing IFormFile with a byte array in an POCO RRS feed

  • Question

  • User-855888433 posted

    Hi,

    I have an HTTP server with a model which has an IFormFile attribute which I want to replace with a byte array. The HTTP Client sends a PUT request with follwing content disposition header:

    "form-data; name=\"File\""

    The body contains raw data.

    On the server side I have following attribute in a POCO model:

    public byte[] File{ get; set; }

    Unfortunatelly this attribute is set to null on the server side. Does anyone have an idea how I can send raw data and receive it in a POCO model?

    Why do I want to do this? I want to save the file into the database and I can't save directly an IFormFile into a database. Therefore I have to open a stream and copy it into an MemoryStream. Then I have to convert it into a byte array. The steps are described here and involve too much copying for my taste.

    Wednesday, November 22, 2017 1:50 PM

All replies

  • User283571144 posted

    Hi StefanoD86,

    Why do I want to do this? I want to save the file into the database and I can't save directly an IFormFile into a database. Therefore I have to open a stream and copy it into an MemoryStream. Then I have to convert it into a byte array. The steps are described here and involve too much copying for my taste.

    According to your description, I suggest you could read the file content in the client side as byte, then convert the file content and other arguments as a poco model.

    Then you could send this poco model(as json) to the server side.

    At last, in the server side, you could use poco model to CRUD.

    Note: you need firstly convert the string to byte array, the store the data into database.

    Best Regards,

    Brando 

    Thursday, November 23, 2017 7:22 AM
  • User-855888433 posted

    Actually this has the same problem that I have to copy too much data. Converting = Copying.

    The client sends a HTTP Multipart via Qt framework:

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
    textPart.setBody("my text");
    
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"File\""));
    QFile *file = new QFile("image.jpg");
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    
    multiPart->append(textPart);
    multiPart->append(imagePart);
    
    QUrl url("http://my.server.tld");
    QNetworkRequest request(url);
    
    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.post(request, multiPart);

    The image part is raw data. How can I bind this with POCO directly as a byte array? This way I don't have to do a conversion which saves copying.

    Thursday, November 23, 2017 6:17 PM
  • User283571144 posted

    Hi StefanoD86,

    The image part is raw data. How can I bind this with POCO directly as a byte array? This way I don't have to do a conversion which saves copying.

    As far as I know, we must convert the form/data to byte when using http to send the file to server.

    If we want to send multiple files to server,  we also need use web api provided MultipartFormDataStreamProvider to split the file.

    Then we could convert the file steam to byte.

    As my posts says if you don't want to achieve this, you need convert the file as byte array in client side and create a poco model. Then the client will send this poco model directly to web api.

    Best Regards,

    Brando

    Friday, November 24, 2017 9:18 AM
  • User-855888433 posted

    This part

    imagePart.setBodyDevice(file);

    reads the data as byte array. Further I'm limited on the client side. I'm using Qt 4.8 on the client side which doesn't support JSON. Never the less. I'm sending the data as byte array. I see the problem on the server side.

    Saturday, November 25, 2017 9:00 AM
  • User1822645077 posted

    Hi,

    Can you try with the latest Qt. I found this article that Qt supports JSON. http://doc.qt.io/qt-5/json.html

    Best regards,

    Zhiqing

    Monday, December 4, 2017 6:51 AM
  • User-855888433 posted

    I can't just simply switch to Qt 4.8. ;-) My employer has to buy licences for it.

    Wednesday, December 6, 2017 9:18 PM
  • User1822645077 posted

    Hi StefanoD86,

    I think Brando ZWZ has provided 2 workarounds which you can go for. I don't think there is any other possibility.

    Cheers,

    Zhiqing

    Monday, December 11, 2017 6:51 AM
  • User1599614677 posted

    Hi, 

    I'm know pass about 2 years, but you can use this code to get ByteArray from IFormFile

    var ms = new MemoryStream();
    uploadedFile.OpenReadStream().CopyTo(ms);
    byte[] Value = ms.ToArray()

    Wednesday, August 28, 2019 7:22 PM