Jump to content

IRequest .Files not being populated


Anthony Musgrove

Recommended Posts

Anthony Musgrove

Hi all - I am having an issue that is consuming most of my time at the moment, and I've been asked to repost the issue here in this forum as it is more relevant here.

 

I have an API endpoint, which is working.   However when I attempt to post a file to the endpoint via <input type="file" name="blah"> with enctype="multipart/form-data", IRequest.Files isn't being populated.

 

 

Lets say the request data from my browser is :-

 

POST /emby/ScripterX/Packages/UploadPackage HTTP/1.1
Host: 192.168.1.50:5999
Connection: keep-alive
Content-Length: 455
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: null
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryCWKRTMWE0OAxwgGf
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

------WebKitFormBoundaryCWKRTMWE0OAxwgGf
Content-Disposition: form-data; name="PkgFile"; filename="testpackage.zip"
Content-Type: application/x-zip-compressed

PK¸·P¿r(Utest123.txtthis is a test, 1 2 3.PK¸·P¿r(U test123.txtPK9?
------WebKitFormBoundaryCWKRTMWE0OAxwgGf
Content-Disposition: form-data; name="btnSubmit"

Upload!
------WebKitFormBoundaryCWKRTMWE0OAxwgGf--

 

 

 

 

 

when handling, you can see File Count = 0.  This is from Request.Files.Length

 

2020-05-24 17:01:33.477 Info HttpServer: HTTP Response 204 to 192.168.1.118. Time: 2ms. http://192.168.1.50:8096/emby/ScripterX/Packages/UploadPackage

2020-05-24 17:01:33.692 Info HttpServer: HTTP POST http://192.168.1.50:8096/emby/ScripterX/Packages/UploadPackage. UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
2020-05-24 17:01:33.694 Info Emby ScripterX: Api File In > File Count = 0

 

 

 

and the summary data for IRequest.Files is:

 

        // Summary:

        //     Access to the multi-part/formdata files posted on this request
        IHttpFile[] Files { get; }
 
 
I utilise IRequiresRequest.  Request data (other then .Files) is correct - content type, content length, etc exists and works fine.
 
thank you in advance,
Anthony
  • Like 1
Link to comment
Share on other sites

Anthony Musgrove

Getting closer to achieving this another way, but surely IRequest is broken (and I'm not going crazy).

 

Another way I'm going about it until its confirmed, which probably won't be reliable.  FileReader() to read the zip file, posting it to the api endpoint as a hidden input, and server-side saving the string content of the zip file to a .zip file.. just comparing.  the top one is the string sent, and the bottom one is the actual content of the zip file... so encoding needs to be looked at.   But I'd rather do it the right way :)

 

5ecb316b4b4b0_zipdatacomparison.png

Link to comment
Share on other sites

Anthony Musgrove

Getting closer now..

 

data:application/x-zip-compressed;base64,UEsDBBQAAAAAAAO4t1C/cihVFgAAABYAAAALAAAAdGVzdDEyMy50eHR0aGlzIGlzIGEgdGVzdCwgMSAyIDMuUEsBAhQAFAAAAAAAA7i3UL9yKFUWAAAAFgAAAAsAAAAAAAAAAQAgAAAAAAAAAHRlc3QxMjMudHh0UEsFBgAAAAABAAEAOQAAAD8AAAAAAA==

Link to comment
Share on other sites

What you should do is have your request object implement IRequiresRequestStream, then access RequestStream directly. When you upload an image using the image editor, this is what happens.

  • Like 1
Link to comment
Share on other sites

Anthony Musgrove

What you should do is have your request object implement IRequiresRequestStream, then access RequestStream directly. When you upload an image using the image editor, this is what happens.

 

Thank you mate, I have been trying to use the RequestStream, I'll look into it a bit more :)   Does the image editor use <form> file inputs to submit/upload to the endpoint, or is it some type of javascript/ajax submission?  Thanks so much

Link to comment
Share on other sites

Anthony Musgrove
so I found where the image editor uploads an image.

 

 

        return connectionManager.getApiClient(currentServerId).uploadItemImage(currentItemId, imageType, file).then(function () {

 

Awesome, and it works exactly how I've just implemented it;

 


        return new Promise(function (resolve, reject) {

            var reader = new FileReader;

            reader.onerror = function () {

                reject()

            },

            reader.onabort = function () {

                reject()

            },

            reader.onload = function (e) {

                var data = e.target.result.split(",")[1];

                instance.ajax({

                    type: "POST",

                    url: url,

                    data: data,

                    contentType: "image/" + file.name.substring(file.name.lastIndexOf(".") + 1)

                }).then(resolve, reject)

            },

            reader.readAsDataURL(file)

 

 

That is fantastic to know :)))))))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...