ServiceAPI / .NET - Can you upload a file from a byte[] array?

Hi all,

I'm wondering whether there's a way to create a new record and upload a file to it using the .NET ServiceAPI interface, but where the file content is contained in a byte[] array rather than an existing file on disk?

It looks like the only option is to write the content to a file and then pass a filename through to the API, which is a bit wasteful when you already have the data available in memory.

Does anyone know whether you can pass this content over to the ServiceAPI via the .NET interface, without needing to write it to a temporary file?

Many thanks!

Parents
  • The TrimClient is only a convenience wrapper around a more full featured client.

    In the code below I am using a FileStream but any stream would do.

    Record record = new Record();
    record.RecordType = new RecordTypeRef() { Uri = 2 };
    
    using (var fs = File.OpenRead("c:\\junk\\test.xml"))
    {
        trimClient.ServiceClient.PostFileWithRequest<RecordsResponse>(fs, "test.xml", record);
    }
  • Many thanks for the info!  Is there a trick to getting this to work?  When I try your code it tells me there is no way to convert a FileStream into a FileInfo.  It looks like the first parameter to PostFileWithRequest can only be a FileInfo instance?

    I think the SDK files came from HPRM 8.11 (the DLLs say they are version 8.1.1.7919.)  Is the SDK I am using too old for this?

    Thanks again for your help.

  • Verified Answer

    Sorry, I was using 9.x, try this:

    Record record = new Record();
    record.RecordType = new RecordTypeRef() { Uri = 2 };
    
    using (var fs = File.OpenRead("c:\\junk\\test.xml"))
    {
         trimClient.ServiceClient.PostFileWithRequest<RecordsResponse>("/Record", fs, "test.xml", record);
    }
  • Perfect!  Thanks so much for your help!

    I created the stream from the array with:

    byte[] content = ...
    Stream fs = new MemoryStream(content);

    Passing this through to the function call as you described seems to work.

    This does seem to pass the file data through as the "upload" parameter rather than the "file" parameter I have used before however, so I'm not sure if this is a problem.

    Unfortunately however I'm now stuck on the normal upload side of things.  It seems that the "Ref" parameters don't get decoded properly and it causes an ArgumentException.  If I put as you have specified:

    record.RecordType = new RecordTypeRef() { Uri = 2 };

    Then the actual HTTP request has that parameter specified as this:

    --636113398464133584
    Content-Disposition: form-data;name="RecordRecordType"
    Content-Type: text/plain;charset=utf-8
    
    {TrimType:RecordType,Uri:1331}

    When I use the ServiceAPI directly, I just put the content as "1331", not "{TrimType..." so I'm assuming this is the source of the ArgumentException.  I can't change the RecordType to something else either, as this:

    record.RecordType = 2;

    Tells me it can't convert int/string/etc. to the RecordTypeRef type.

    What's the correct way to pass the RecordType and Container parameters?  Sorry for such a basic question but I couldn't find an example for uploading a file through the .NET ServiceAPI interface.  I did look at hprm.info but I could only find one example for uploading a file with this method, and unfortunately it was a Vimeo video and no videos from there play on my machine so I'm not sure if that would answer my question or not!  It was for HPRM 83 so not sure if that would work with 8.11 if that's what I'm on.

    Thanks again for your help.

  • Perfect!  Thanks so much for your help!

    I created the stream from the array with:

    byte[] content = ...
    Stream fs = new MemoryStream(content);

    Passing this through to the function call as you described seems to work.

    This does seem to pass the file data through as the "upload" parameter rather than the "file" parameter I have used before however, so I'm not sure if this is a problem.

    Unfortunately however I'm now stuck on the normal upload side of things.  It seems that the "Ref" parameters don't get decoded properly and it causes an ArgumentException.  If I put as you have specified:

    record.RecordType = new RecordTypeRef() { Uri = 2 };

    Then the actual HTTP request has that parameter specified as this:

    --636113398464133584
    Content-Disposition: form-data;name="RecordRecordType"
    Content-Type: text/plain;charset=utf-8
    
    {TrimType:RecordType,Uri:1331}

    When I use the ServiceAPI directly, I just put the content as "1331", not "{TrimType..." so I'm assuming this is the source of the ArgumentException.  I can't change the RecordType to something else either, as this:

    record.RecordType = 2;

    Tells me it can't convert int/string/etc. to the RecordTypeRef type.

    What's the correct way to pass the RecordType and Container parameters?  Sorry for such a basic question but I couldn't find an example for uploading a file through the .NET ServiceAPI interface.  I did look at hprm.info but I could only find one example for uploading a file with this method, and unfortunately it was a Vimeo video and no videos from there play on my machine so I'm not sure if that would answer my question or not!  It was for HPRM 83 so not sure if that would work with 8.11 if that's what I'm on.

    Thanks again for your help.

  • Perfect!  Thanks so much for your help!

    I created the stream from the array with:

    byte[] content = ...
    Stream fs = new MemoryStream(content);

    Passing this through to the function call as you described seems to work.

    This does seem to pass the file data through as the "upload" parameter rather than the "file" parameter I have used before however, so I'm not sure if this is a problem.

    Unfortunately however I'm now stuck on the normal upload side of things.  It seems that the "Ref" parameters don't get decoded properly and it causes an ArgumentException.  If I put as you have specified:

    record.RecordType = new RecordTypeRef() { Uri = 2 };

    Then the actual HTTP request has that parameter specified as this:

    --636113398464133584
    Content-Disposition: form-data;name="RecordRecordType"
    Content-Type: text/plain;charset=utf-8
    
    {TrimType:RecordType,Uri:1331}

    When I use the ServiceAPI directly, I just put the content as "1331", not "{TrimType..." so I'm assuming this is the source of the ArgumentException.  I can't change the RecordType to something else either, as this:

    record.RecordType = 2;

    Tells me it can't convert int/string/etc. to the RecordTypeRef type.

    What's the correct way to pass the RecordType and Container parameters?  Sorry for such a basic question but I couldn't find an example for uploading a file through the .NET ServiceAPI interface.  I did look at hprm.info but I could only find one example for uploading a file with this method, and unfortunately it was a Vimeo video and no videos from there play on my machine so I'm not sure if that would answer my question or not!  It was for HPRM 83 so not sure if that would work with 8.11 if that's what I'm on.

    Thanks again for your help.

  • Ok I feel silly now...  After some more investigating, the problem turned out to be that I was using the wrong record type Uri, which was a record type that was a container only and did not allow electronic documents to be added to it.

    Once I changed the record type Uri to the correct value, everything worked!  So the funny values and the content being called "upload" instead of "file" seems to make no difference - the ServiceAPI seems to accept it without any complaint!

  • Hi,

    Is it possible to do this somehow with 8.2?

  • I do not have an 8.2 instance handy at the moment but you should be able to do something similar to what works with 9.x.  I think that instead of trimClient.ServiceClient it might be trimClient.JsonClient, or something similar.

Reply Children