Jump to content

Recommended Posts

Posted (edited)

Hey guys, currently trying to post an Image to a upload field of a database.

Getting the following error :

 

1S306/E	UPLOAD_FIELD_NOT_OBJECT	Field of type Upload was supplied without being set as an object
            RestRequest restRequest = new RestRequest("/api/cms/records/" + DatabaseID.ToString() + "/" + "1");
            restRequest.Method = Method.POST;
            restRequest.AddHeader("cache-control", "no-cache");
            restRequest.AddHeader("Authorization", $"Bearer {Token.GetValue("access_token")}");
            byte[] FileContent = File.ReadAllBytes("C:\\Users\\UserName\\Desktop\\Capture1.png");
            var Mydate = new
            {
                SS = FileContent
            };
            var Image1 = JsonConvert.SerializeObject(Mydate);
            var Image = JsonConvert.DeserializeObject<JObject>(Image1);
              restRequest.AddParameter("fields[276]", Image);                        
            var response = Client.Post(restRequest);



Any suggestions would be great thanks.

Edited by InfinityRazz
Posted
Quote

For fields of the Upload type, the type of the value must also be an object itself, with each key set as the filename and the value set as the raw file contents.

This is the important part of the documentation for this issue.

Your field needs to be "fields[276][filename]"

Posted (edited)

@Stuart Silvester Thanks for the quick response as always I did read over the API reference a few times but still don't fully understand what I am doing wrong here.

Thanks to your suggestion - I am not no longer getting error that specific error but instead : 

    "errorCode": "1T306\/H",
    "errorMessage": "UPLOAD_FIELD_IMAGE_NOT_SUPPORTED"

I am not too sure what exactly is meant by "raw file contents" I've tried reading and setting the image as a Byte Array and as well as Base64String but still get the above error...

My code block for reference :

 

            RestRequest restRequest = new RestRequest("/api/cms/records/" + DatabaseID.ToString() + "/" + "1");
            restRequest.Method = Method.POST;
            restRequest.AddHeader("cache-control", "no-cache");
            restRequest.AddHeader("Authorization", $"Bearer {Token.GetValue("access_token")}");
            byte[] FileContent = File.ReadAllBytes("C:\\Users\\UserName\\Desktop\\Capture1.png");
            var Mydate = new
            {
                SS =   FileContent   // Convert.ToBase64String(FileContent)
            };
            var Image1 = JsonConvert.SerializeObject(Mydate);
            var png = JsonConvert.DeserializeObject<JObject>(Image1);
              restRequest.AddParameter("fields[276][SS]", png);                
            var response = Client.Post(restRequest);
            Console.WriteLine(response.Content);    

Minor edit - I've tried this with both jpg and png image formats.

Edited by InfinityRazz
Posted (edited)

Thanks - I did try the previously and got the error : 
 

Quote

UPLOAD_FIELD_IMAGES_ONLY



After changing the field to allow any uploads (not only images)

I get the following :
 

Quote

    "errorCode": "EX0",
    "errorMessage": "UNKNOWN_ERROR"

I get the above error when attempting to send the file as Base64 String or as a Byte Array.

Edited by InfinityRazz
Posted

What is the error that is showing in your system logs relating to that? If not, please leave it with me and will try somehting in .net myself to see whats happening. Which database are you using? I can then download a copy of that database and use that for testing

Posted

@Marc Stridgen Thanks mate - The Database I am using is WEPW-viewer (id-42 with one only one record)

My system error logs for this API request result in the following :

 

#0 /var/www/html/106167/system/File/File.php(326): IPS\_Image::create('System.Byte[]')
#1 /var/www/html/106167/applications/cms/api/records.php(501): IPS\_File::create('cms_Records', 'ss.png', 'System.Byte[]')
#2 /var/www/html/106167/applications/cms/api/records.php(386): IPS\cms\api\_records->_createOrUpdate(Object(IPS\cms\Records42), 'edit')
#3 /var/www/html/106167/system/Api/Controller.php(180): IPS\cms\api\_records->POSTitem(Object(IPS\cms\Databases), Object(IPS\cms\Records42))
#4 /var/www/html/106167/system/Dispatcher/Api.php(329): IPS\Api\_Controller->execute(Array, false)
#5 /var/www/html/106167/api/index.php(11): IPS\Dispatcher\_Api->run()
#6 {main}

If you need anything else just give me a shout ! 

Posted

See if you have any luck with this for getting the byte array for your file

 

byte[] yourFile = FileToByteArray("C:\\Users\\UserName\\Desktop\\Capture1.png");  

      public byte[] FileToByteArray(string fileName)
        {
            byte[] buff = null;
            FileStream fs = new FileStream(fileName,
                                           FileMode.Open,
                                           FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            return buff;
        }

 

Posted

So in your case, you would be placing

var Mydate = new
            {
                SS =   FileContent   // Convert.ToBase64String(FileContent)
            };
            var Image1 = JsonConvert.SerializeObject(Mydate);
            var png = JsonConvert.DeserializeObject<JObject>(Image1);

With

 

byte[] png = FileToByteArray("C:\\Users\\UserName\\Desktop\\Capture1.png"); 

and adding the function to your project

      public byte[] FileToByteArray(string fileName)
        {
            byte[] buff = null;
            FileStream fs = new FileStream(fileName,
                                           FileMode.Open,
                                           FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            return buff;
        }

 

Posted

I am not entirely sure if you're suggesting I send the raw bytes of the .png such as :
 

  restRequest.AddParameter("fields[276][Test.png]", Bytes[]);    

 Or the Json Object 

            var Mydata = new
            {
                Capture1 =  FileContent   ///Convert.ToBase64String(FileContent)
            };
            var Image1 = JsonConvert.SerializeObject(Mydata);
            var jpg = JsonConvert.DeserializeObject<JObject>(Image1);
              restRequest.AddParameter("fields[276][Test.png]", jpg);

I've done both and my system errors logs are slightly different. 

Posting the Byte Array  :

 

#0 /var/www/html/106167/system/File/File.php(326): IPS\_Image::create('System.Byte[]')
#1 /var/www/html/106167/applications/cms/api/records.php(501): IPS\_File::create('cms_Records', 'Test.png', 'System.Byte[]')
#2 /var/www/html/106167/applications/cms/api/records.php(386): IPS\cms\api\_records->_createOrUpdate(Object(IPS\cms\Records42), 'edit')
#3 /var/www/html/106167/system/Api/Controller.php(180): IPS\cms\api\_records->POSTitem(Object(IPS\cms\Databases), Object(IPS\cms\Records42))
#4 /var/www/html/106167/system/Dispatcher/Api.php(329): IPS\Api\_Controller->execute(Array, false)
#5 /var/www/html/106167/api/index.php(11): IPS\Dispatcher\_Api->run()
#6 {main}



Posting the Json Object :

 

#0 /var/www/html/106167/system/File/File.php(326): IPS\_Image::create('{\r\n  "Capture1"...')
#1 /var/www/html/106167/applications/cms/api/records.php(501): IPS\_File::create('cms_Records', 'Test.png', '{\r\n  "Capture1"...')
#2 /var/www/html/106167/applications/cms/api/records.php(386): IPS\cms\api\_records->_createOrUpdate(Object(IPS\cms\Records42), 'edit')
#3 /var/www/html/106167/system/Api/Controller.php(180): IPS\cms\api\_records->POSTitem(Object(IPS\cms\Databases), Object(IPS\cms\Records42))
#4 /var/www/html/106167/system/Dispatcher/Api.php(329): IPS\Api\_Controller->execute(Array, false)
#5 /var/www/html/106167/api/index.php(11): IPS\Dispatcher\_Api->run()
#6 {main}



It's worth noting that I did both these tests using your provide FileToByteArray function.

Posted

Sorry, "placing" above should read "replacing". For example

 

byte[] png = FileToByteArray("C:\\Users\\UserName\\Desktop\\Capture1.png");
restRequest.AddParameter("fields[276][Test.png]", png);

 

Note I havent tested this myself as of yet. If you still have problems with that, you would need to leave it with me while I have chance to take a look.

Posted
On 4/28/2022 at 2:53 AM, Marc Stridgen said:

Is that restsharp you are using I assume?

Hey there marc! Other admin of this site/account here.
Here is the code i've been playing around with most of the afternoon: 
I have tried getting files from FileStream and/or from embedded resources streams, with the exact same results.

var endpoint = $"/api/cms/records/40/2";
var website = "https://REPLACEME.forumflash.com";

var client = new RestClient(website);
var request = new RestRequest(endpoint);
request.AddParameter("key", token); // Token mentioned here is "Toxic Poster"
request.AddParameter("random", new Random().Next(1, 500000));

var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("ConsoleApp1.Untitled.png"))
// using (var stream = assembly.GetManifestResourceStream("ConsoleApp1.idTip-10.1.1-retail.zip"))
{
    var buffer = new byte[stream.Length];
    var t = stream.ReadAsync(buffer, 0, buffer.Length);
    t.Wait();

    Console.WriteLine($"Stream Length   -> {stream.Length}\nBuffer Length   -> {stream.Length}");
    request.AddParameter($"fields[277][Untitled.bmp]", buffer);
}

var response = client.ExecutePostAsync(request);
response.Wait();

Console.WriteLine($"{response.Result.ResponseStatus}");
Console.WriteLine($"{response.Result.Content}");

Right now it "Sort of" works. I have tested the above with .bmp (and the commented with a .zip) file extensions, as well as posting synchronously as well as asynchronously with the same result. The file "appears" to be uploaded, but upon editing the record, or downloading the file, it is only 13bytes in size while the original source varies from 12k(.zip) to 4k(.bmp). 
Right now this upload field accepts everything, and throws the errors mentioned above if i try to upload a .png/jpeg/gif
UNKNOWN_ERROR

If i edit this upload field to only accept images, it throws the errors mentioned above: 
UPLOAD_FIELD_IMAGES_ONLY if i use a .png/jpeg/gif extension or
UPLOAD_FIELD_IMAGE_NOT_SUPPORTED if i try to post as a .bmp/.svg extension(guess no bitmaps 😞 )

Not sure what i'm doing wrong here, Any guidance you could provide at this time is greatly appreciated!
And yes! I'm using Restsharp 106.15.0, have also tried with 107.3.0 with the exact same results.

send help GIF

My "workaround" at this point would be to just upload an image to a text field as a Base64 encoded string, then write a JS converter to deserialize it but... would rather not do it the janky way 😉

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...